What's the issue in a code written for comparing the date with today's date?

▼魔方 西西 提交于 2019-11-28 02:30:15

If posted format is in m-d-Y, then you cannot convert it to unix timestamp directly with strtotime() function, because it will return false.

If you need to use strtotime() then change the input format to m/d/Y by simple str_replace().

On the other hand, you could use DateTime class, where you can directly compare objects:

$submission_date = DateTime::createFromFormat('!m-d-Y', $submission_date);
$today_date = new DateTime('today');

if ($submission_date > $today_date) {
    echo "submission_date is in the future\n";
}

demo

If you need to extract some information from DateTime objects, use format() method on them, which accepts same format as date() function:

echo $today_date->format('m/d/Y'); # 12/11/2014
echo $today_date->format('m-d-Y'); # 12-11-2014
echo $today_date->format('Y-m-d'); # 2014-12-11
echo $today_date->format('Y-Y-Y'); # 2014-2014-2014

demo

I think you need to compare date in 'Y-m-d' or 'd-m-Y' format. I think it is not possible to compare date in 'm-d-Y' format

As we have made changes in your code and we test it works from my side so can you try below.

$submission_date = $_POST['submission_date'];
$current_date = date('d-m-y H:i:s');
if (strtotime($submission_date) > strtotime($current_date))
{
   echo "Future date not accepted";
}

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!