Jump to content

[SOLVED] Help working with PHP and Dates


chinclub

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/62838-solved-help-working-with-php-and-dates/
Share on other sites

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;
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.