SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Date problems

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date problems

    I'm having trouble with some date functionality which I'm hoping someone will be able to help me with.

    The scenario is that I'm being given data which has a creation date. The problem is that the creation date is calculated by the number of hours passed from a pre-defined date, in this case 1900-01-01.

    I've tried a couple of methods to work this out, my first looks like this:

    [code=php]
    $datefrom = 957047;
    $this->strDate = (date('Y-m-d H:00:00', strtotime('+'.$datefrom.' hours', date_format(date_create('1900-01-01'), 'U')))."\n");
    [/code]

    This ends up printing out '2011-02-16 19:00:00'
    The date I should be getting should be something in the region of '2009-01-02'
    This method is basically trying to use strtotime to increase the given date by the given number of hours.

    My second method looks like this:

    [code=php]
    $datefrom = 957047;
    $secondsSince1900 = date_format(date_create('1900-01-01'), 'U');
    $datefromInSeconds = $datefrom*60*60;

    $SecondsAdded = $secondsSince1900+$datefromInSeconds;

    $newDate = date_create($SecondsAdded);

    echo("dateformat: ".date('Y-m-d H:00:00', $SecondsAdded)."\n");
    [/code]

    This one's getting a bit closer with a result of '2009-03-06 23:00:00'
    I've converted the hours passed into seconds and added that to the timestamp, then I've converted it into a date format.

    I did notice that if I change the '$datefrom' to 0, then the result is actually '1901-12-13 20:00:00' instead of '1900-01-01' which it should still be as it's just changing the date from a timestamp and back to a date format.

    Any help would be fantastic.

    Thanks in advance.

  2. #2
    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)
    If you're just adding hours to a time stamp, could you not just use the following?

    PHP Code:
    <?php
    function add_hours_to_date($fHours$iTimestamp)
    {
        return  
    $iTimestamp + (($fHours 60) * 60);
    }

    $iNow time();
    echo 
    date('r'$iNow); #Mon, 11 May 2009 09:52:14 +0100

    $iNow add_hours_to_date(3.5$iNow);
    echo 
    date('r'$iNow); #Mon, 11 May 2009 13:22:14 +0100
    ?>
    @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.

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, that looks like it's essentially what my second block of code is doing, just inline instead of a function.

    PHP Code:
    $datefrom 957047;
    $secondsSince1900 date_format(date_create('1900-01-01'), 'U');
    $datefromInSeconds $datefrom*60*60;
    $SecondsAdded $secondsSince1900+$datefromInSeconds;
    $newDate date_create($SecondsAdded);
    echo(
    "dateformat: ".date('Y-m-d H:00:00'$SecondsAdded)."\n"); 
    I don't think this is giving the correct result though.... it's returning '2009-03-06 23:00:00' when I've been led to believe that the result should be '2009-01-02'...

    I shall have to try this method with some other data to see what the results are like.

    Thanks again.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    SilverBulletUK: The way I read it, he's being given a date in the form of "hours since 1900-01-01", which is not the same as adding hours to a timestamp.

    The problem is that 1900-01-01 is before the Unix epoch, so you can't use date_format('U') to turn it into a timestamp to add hours to. It's before time 0.

    So you need to do some math yourself before you can work with the number as a timestamp.

    Start by figuring out how far from 1900-01-01 time 0 (1970-01-01) is, and subtracting that from your $datefrom

    PHP Code:
            $datefrom 957047;

            
    $base 70 365.25 24 60 60;
            
    $timestamp = ($datefrom 60 60) - $base;

            echo 
    date('Y-m-d'$timestamp); 
    Now you have a Unix timestamp for the creation date (2009-03-06).

    Does that sound right?

    I'm not sure what to do about the leap years though. Multiplying by 365.25 is a hack that could leave you a day off..

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    SilverBulletUK: The way I read it, he's being given a date in the form of "hours since 1900-01-01", which is not the same as adding hours to a timestamp.

    The problem is that 1900-01-01 is before the Unix epoch, so you can't use date_format('U') to turn it into a timestamp to add hours to. It's before time 0.

    So you need to do some math yourself before you can work with the number as a timestamp.

    Start by figuring out how far from 1900-01-01 time 0 (1970-01-01) is, and subtracting that from your $datefrom

    PHP Code:
            $datefrom 957047;

            
    $base 70 365.25 24 60 60;
            
    $timestamp = ($datefrom 60 60) - $base;

            echo 
    date('Y-m-d'$timestamp); 
    Now you have a Unix timestamp for the creation date (2009-03-06).

    Does that sound right?

    I'm not sure what to do about the leap years though. Multiplying by 365.25 is a hack that could leave you a day off..


    Well that's something.... it's a different method but gives the same result to what I was getting....... it's possible I was given an incorrect result to compare it to.

    I shall look into this

    Thanks

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It can't be 2009-01-02...

    957047 / 24 = 39876.96 days
    39875.96 / 365 = 109.25 years

    A quarter of a year would put you in March, not January

  7. #7
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    It can't be 2009-01-02...

    957047 / 24 = 39876.96 days
    39875.96 / 365 = 109.25 years

    A quarter of a year would put you in March, not January
    A very good point, I've asked the data provider on their thoughts on this.

  8. #8
    SitePoint Member
    Join Date
    May 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    It can't be 2009-01-02...

    957047 / 24 = 39876.96 days
    39875.96 / 365 = 109.25 years

    A quarter of a year would put you in March, not January
    I've received word that the date within some of the files is incorrect. The date is also encoded within the filename which is correct and safe to use.

    Some people!

    It's a good thing the file I was testing contained faulty data so it could be caught at this stage of the project!

    Thanks for all the help.

  9. #9
    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)
    What about the DateTime object? Is this viable?
    PHP Code:
    <?php
    $oDate 
    = new DateTime('01-01-1900', new DateTimeZone('Europe/London'));

    $oDate->modify(
        
    sprintf(
            
    '+ %s Hours',
            
    278324
        
    )
    );

    echo 
    $oDate->format('r'); #Fri, 02 Oct 1931 20:00:00 +0100
    ?>
    @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.


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
  •