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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.