Page 1 of 1

Setting and comparing date-time

Posted: Thu Mar 19, 2009 9:04 pm
by vbmark
Hello,

I need to set a datetime stamp to an arbitrary number of days in the future (to the minute) set by the user, i.e. Add 5 days to now. I'm going to save this future date in my database.

Then, when they hit a certain page in the future I need to pull that value and see if the current time, (down to the minute, not second) is before or after the datetime they entered.

I am not sure in what format to store the datetime stamp in order to do the comparison later.

And how do you compare datetime stamps?

Here is what I've got so far:

Code: Select all

echo date('Y-m-d-H-i', strtotime("+2 days"));
I don't think that is the best way to do it.

Thanks!

Re: Setting and comparing date-time

Posted: Fri Mar 20, 2009 1:04 am
by jaoudestudios

Code: Select all

$tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));

Code: Select all

 
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
      -> '1997-12-02'
 

Re: Setting and comparing date-time

Posted: Fri Mar 20, 2009 8:44 pm
by vbmark
Thanks for the information.

I got it to work in PHP but when I send it to my database I get all Zeros. Here is what I am doing.

Code: Select all

$days = 8;
$endingdate = mktime(date("H"), date("i"), 0, date("m"), date("d") + $days, date("Y"));
Then I am putting ending date into a MySQL timestamp field.

The results in the table are all: 0000-00-00 00:00:00

Any idea what I might be doing wrong?

Thanks!

Re: Setting and comparing date-time

Posted: Sat Mar 21, 2009 4:31 am
by jaoudestudios
mktime returns a php unix timestamp i.e. 1237593600

You will need to reformat it, either in php or mysql to the mysql timestamp format.