thecase Posted June 29, 2009 Share Posted June 29, 2009 Hi, I have the date like this Friday 26 June 2009 how can I create a if statment to check if that time is the present or in the past Thanks Quote Link to comment https://forums.phpfreaks.com/topic/164137-solved-date-comparison/ Share on other sites More sharing options...
Maq Posted June 29, 2009 Share Posted June 29, 2009 Hi, I have the date like this Friday 26 June 2009 how can I create a if statment to check if that time is the present or in the past Thanks Checks if $day matches the current day. Something like this: $day = "Monday 29 June 2009"; $now = date("l j F Y"); echo ($day==$now) ? "today" : "not today"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/164137-solved-date-comparison/#findComment-865842 Share on other sites More sharing options...
thecase Posted June 29, 2009 Author Share Posted June 29, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/164137-solved-date-comparison/#findComment-865851 Share on other sites More sharing options...
HPWebSolutions Posted June 29, 2009 Share Posted June 29, 2009 The above solution is good if you want to determine if the input date is the current date, but not to determine if it is a past date since it may also be a future date. You can use strtotime to extract a timestamp from the input date and then compare the timestamps of the input date and the current date. Here is a simple solution: $new_date = strtotime('Friday 26 June 2009'); $current_date = strtotime(date('l j F Y')); if($new_date < $current_date) echo 'input date is a previous date'; elseif($new_date > $current_date) echo 'input date is a future date'; elseif($new_date == $current_date) echo "input date is today's date"; Quote Link to comment https://forums.phpfreaks.com/topic/164137-solved-date-comparison/#findComment-865862 Share on other sites More sharing options...
Maq Posted June 29, 2009 Share Posted June 29, 2009 how can I create a if statment to check if that time is the present or in the past He only specified present or past. So I think it's safe to speculate that he doesn't allow users to choose a date in the future, but if he does, then good point. Quote Link to comment https://forums.phpfreaks.com/topic/164137-solved-date-comparison/#findComment-865868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.