chinclub Posted August 1, 2007 Share Posted August 1, 2007 I need a little help writing a piece of code. I have added a $lastvisit field to my database table as type date. So it records as YYYY-MM-DD. I can't seem to figure out the PHP code to figure the time between dates. I want to have the database retrieve the $lastvisit then have PHP code check that date against todays date and figure how many days it has been as something like: $timelapse = $NowDate - $lastvisit; I know that isn't correct (as I have tried it and just gives $timelapse = 0) and all of the date info I can find tells how to call info from the database by date but not how to figure time between dates. I want to be able to take that $timelapse and then return different responses for different dates by if ($timelapse > 2){print"whatever";} Can anyone point me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/62838-solved-help-working-with-php-and-dates/ Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 You need to convert your dates to unix timestamps, either using the mysql UNIX_TIMESTAMP() function, or with the php strtotime() function to allow you to make a comparison: <?php $date1 = '2007-08-01'; $date2 = '2006-08-01'; $diff= strtotime($date1) - strtotime($date2); $days = $diff/(60*60*24); echo $days; ?> Quote Link to comment https://forums.phpfreaks.com/topic/62838-solved-help-working-with-php-and-dates/#findComment-312820 Share on other sites More sharing options...
chinclub Posted August 1, 2007 Author Share Posted August 1, 2007 Thank-you so much!! Quote Link to comment https://forums.phpfreaks.com/topic/62838-solved-help-working-with-php-and-dates/#findComment-312823 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.