Date problems

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
wispa
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 7:41 am

Date problems

Post by wispa »

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: Select all

 
$datefrom = 957047;
$this->strDate = (date('Y-m-d H:00:00', strtotime('+'.$datefrom.' hours', date_format(date_create('1900-01-01'), 'U')))."\n");
 
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: Select all

 
$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");
 
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.
Last edited by Benjamin on Sun May 10, 2009 12:39 pm, edited 1 time in total.
Reason: Changed code type from text to php.
wispa
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 7:41 am

Re: Date problems

Post by wispa »

I think I've found out why the first set of code isn't working, something to so with the strtotime function only works with a date later than 1901-12-13. Still not sure why the second set isn't working.
Post Reply