...

View Full Version : strtotime(), date(), and time()



Corey
11-24-2011, 12:21 AM
Hi guys,

I'm having a couple of issues with using strtotime() (http://php.net/manual/en/function.strtotime.php), date() (http://www.php.net/manual/en/function.date.php), and time() (http://www.php.net/manual/en/function.time.php).

The first is: I used strtotime() (http://php.net/manual/en/function.strtotime.php) to get the unix of a specific date and time (worked great, even outputted the correct time using date() (http://www.php.net/manual/en/function.date.php) to return it). However when I went to post information on it (I set it up so that you can't post after the event started), it told me it already started like an hour before it was supposed to. I used


if (time() > $event['when']) { echo "too late."; } else { do it }
to determine whether you can post or not. That had me very confused.

Then I decided instead of manually inputting the event times I would pull them from an xml file, which I had no trouble reading.

I used:
$day = "Sun";
$time = "1:00";
strtotime("Next $day, $time PM")

This outputted a unix timestamp but then date() (http://www.php.net/manual/en/function.date.php) returned it as being 7 pm on sunday instead.

Any ideas what would be causing these strange things?

tangoforce
11-24-2011, 12:43 AM
strtotime() is pretty strict about the format of the date you put in as a string.

See this page of formats. (http://uk3.php.net/manual/en/datetime.formats.date.php) Look at the relative formats.

I *think* you need to skip the comma and PM and use the 24hr clock.

Fou-Lu
11-24-2011, 02:39 AM
Have you specified the date_default_timezone_set as well? Functions like date() are locale aware and will format according to their timezone. These are often UTC.

PHP's documentation for strtotime is terrible honestly. Use this link instead: http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html
strtotime should ignore the comma.

Corey
11-24-2011, 11:02 PM
Thanks for the replies. The problem was I had the timezone set in the header of the config file. I found out that it had to be assigned within the static method.


static function Game_Info($game, $options = NULL)
{
date_default_timezone_set('EST');
if (is_array($game))
{
if (isset($options['shorten_team']))
{
$game['home_team'] = self::team_name($game['home_team']);
$game['away_team'] = self::team_name($game['away_team']);
}
if (isset($options['date']))
{
$game['when'] = date('D M j G:i:s T Y', $game['when']);
}
else
{
$game['when'] = date('D \@ g\:i A', $game['when']);
}
return $game;
}
}
static function Get_All($group = NULL, $options = NULL)
{
$return = array();
$time_sec = strtotime(date('m') . '/' . (date('d') - 2) . '/' . date('Y'));
$get_games = Sports_Database::Build_Query('SELECT', 'games', "`when` > '" . $time_sec . "'");
while ($game = $get_games->FetchRow())
{
if ($group == 'lid')
{
$return[$game['league_id']][] = self::Game_Info($game, $options);
}
else
{
$return[] = self::Game_Info($game);
}
}
return $return;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum