Timing always returning false

Hey,

I’m pretty stumped here. I’m adding an additional index and setting it to true if a condition is met. The condition is based of the time and 30 minutes ahead of time.


$date = new DateTime();
$date->setTimestamp(time());

$row['editable'] = false;

if ((strtotime($row['date']) + 1800 > $date->getTimestamp())) {
  $row['editable'] = true;
}

What I’m attempting to do is add 1800 seconds(30 minutes) of the row[‘date’] in order to run it against the current time stamp. But this condition is never met.

Am I misunderstanding how unix time stamp works? I’ve looked all over and found that I seem to be doing it correctly.

I did do the span of 24 hours:

ie. 24 * 60 * 60

and it worked correctly, so I’m not sure where I went wrong. I’m looking for 30 minutes only, which is 1800.

My database seems correct but my PHP time is off by 4 hours it seems. I did the following:


$timezone = new DateTimeZone('America/New_York');
$date = new DateTime('now');
$date->setTimezone($timezone);
        
echo $_date . ' time: ' . date('Y-m-d H:i:s', $date->getTimestamp()) . '<br />';

…and it still isn’t adjusting correctly. First time, I’ve ever experienced this.

EDIT:

I added the time zone abbreviation and it seems it’s running on UTC + 4 so I adjusted it (-14400) and it seems to be working correctly now. I find it weird that it won’t adjust the time difference when setting the timezone object.

Maybe you can post a test case to reproduce it? The following works as expected.


<?php
$row['date'] = '2010-07-11 22:30:00';
if(strtotime('+ 30 minutes', strtotime($row['date'])) > time()){
  #editable
}
?>

Are your database and server reported datetimes the same?

I tried that as well earlier today, and it returned false.

I thought maybe strtotime function couldn’t parse the format I’m giving it, but after verifying on the manual, it seems it can.

Format of row[‘date’] is: 0000-00-00 00:00:00

Both servers are running on my local machine.

echo $row[‘date’] . ’ timestamp: ’ . date(‘Y-m-d H:i:s’, time()) . ‘<br />’;

output at the time:

2010-07-11 17:46:21 timestamp: 2010-07-11 21:47:25

Always seem to be 4 hours ahead for some reason?

It seems that it works correctly if I hard code the time into it like you did. But it doesn’t seem to work if it’s dynamically coming from the database.

EDIT:

I ran this:

1278870381 time(): 1278885224

the difference between the two at the time is 14, 843 seconds. About 4 hours of a difference. hmm weird.


<?php
if(strtotime('+ 30 minutes', strtotime($row['date'])) > time()){
  #editable
}
?>