SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: Date problems
-
May 11, 2009, 01:45 #1
- 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.
-
May 11, 2009, 01:53 #2
- 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.
-
May 11, 2009, 02:00 #3
- 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);
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..Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 11, 2009, 02:04 #4
- 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 shall have to try this method with some other data to see what the results are like.
Thanks again.
-
May 11, 2009, 02:07 #5
- Join Date
- May 2009
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 11, 2009, 02:07 #6
- 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 JanuaryTry Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
May 11, 2009, 02:28 #7
- 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.
-
May 11, 2009, 02:31 #8
- Join Date
- May 2009
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
May 12, 2009, 08:59 #9
- Join Date
- May 2009
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
Bookmarks