SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reliably handling dates in PHP

    I am trying to work with scheduled tasks in PHP and I need to make sure that tasks are executed at the correct times relative to a user's timezone. I also need to take my server's timezone into account.

    For example, I know that the user wants the task to execute on Monday 20 July 2009 at 12:30pm and I know their timezone offset is 14400 (5 hours) behind GMT.

    Will this be reliable:
    PHP Code:
    $ex_time strtotime("Monday 20 July 2009 12:30pm"); //Convert to time integer
    $ex_time $ex_time $_SERVER['time_offset']; // Adjust for our server's timezone
    $ex_time $ex_time 14400// Adjust for our user's timezone 
    Say it gives me a result such as 1248498000.

    Can I use it like this when I am executing the tasks:
    PHP Code:
    if($ex_time >= time())
    {
    //execute task

    Basically I want to know if converting dates and times to a time() integer and then adjusting for the timezone is the best way to handle this. Or is there a better way to handle this to keep times consistent?

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am pretty sure you should first set server's timezone, then set user's timezone.

    For example, in php.ini set:
    date.timezone = "US/Central"

    Then set user's timezone (you need to ask user to choose his timezone and then store it in user's profile or something like that)
    Set user's timezone in beginnig of script:

    date_default_timezone_set('America/Chicago');

  3. #3
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically to answer your question - the way you trying to handle timezone difference is a hard way.

    The easy way is to set user's timezone like this:
    date_default_timezone_set('America/Chicago');

    That's all you need to do! Php will then adjust all time related calculations to user's timezone, so you don't have to worry about that.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    http://us3.php.net/manual/en/class.datetime.php

    PHP Code:
    $user_date 'Monday 20 July 2009 12:30pm';
    $user_zone 'America/Chicago';

    $date = new DateTime$user_date, new DateTimeZone$user_zone ) );
    echo 
    $date->formatDateTime::ISO8601 ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    http://us3.php.net/manual/en/class.datetime.php

    PHP Code:
    $user_date 'Monday 20 July 2009 12:30pm';
    $user_zone 'America/Chicago';

    $date = new DateTime$user_date, new DateTimeZone$user_zone ) );
    echo 
    $date->formatDateTime::ISO8601 ); 


    Much better than messing around with ini settings.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your initial example will calculate the time in the timezone the server is in. That's why if you're trying to work accurately with timezones and you know what timezone the user is in (i.e. they have specified it), then use gmmktime as your base, and then add the offset.

    Or... you can use DateTime.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •