Setting and comparing date-time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vbmark
Forum Newbie
Posts: 19
Joined: Sun Feb 15, 2009 8:53 pm

Setting and comparing date-time

Post 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!
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Setting and comparing date-time

Post 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'
 
vbmark
Forum Newbie
Posts: 19
Joined: Sun Feb 15, 2009 8:53 pm

Re: Setting and comparing date-time

Post 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!
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Setting and comparing date-time

Post 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.
Post Reply