Forum Moderators: coopster

Message Too Old, No Replies

Script returning the wrong date sometimes

         

nickCR

5:24 am on Apr 15, 2010 (gmt 0)

10+ Year Member



Hello All,

So I have a function that accepts a date the format and desired time zone. I have included the function below.

The function works 95% of the time, however for some weird reason which I cannot explain some days around 10:30 PM (MST) it doesn't return the right date.

For example tonight I was having a problem. So I checked the result from the function and it was returning 2010-04-15 10:04:00 when it should have returned 2010-04-14 22:04:00. I can't explain why sometimes it does this. Anyone else have had this problem before?

In the example above $date_str is 2010-04-15 12:04:00 (EST), trying to convert it to (MST) but the timezone needs to be dynamic.

If I remove the $time from the date so it renders now it works fine. The problem would appear to be with strtotime.


function convert_tz ($date_str, $format, $tz) {

$time = strtotime($date_str);

putenv("TZ={$tz}");
$ret = date($format, $time);
putenv("TZ=America/New_York");

return $ret;

}


Thanks in advance,

Nick

Matthew1980

12:35 pm on Apr 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there nickCR,

How are you generating the $date_str? And what is the value in $format when it gets passed?

I use strtotime() for server time adjustments, but only for +/- a few hours, as yet no issues, but you have me intrigued.

Cheers,
MRb

CyBerAliEn

4:18 pm on Apr 15, 2010 (gmt 0)

10+ Year Member



^Yes; elaborate on the input to your function (examples). It is reasonable something is up with the input.

nickCR

4:44 pm on Apr 15, 2010 (gmt 0)

10+ Year Member



Hi guys thanks for your replies.

On the specific page that I am having the problem the function is called as such:


$dateMST = $ci->convert_tz(date("Y-m-d h:m:s"), "Y-m-d", $coupon['timeZone']);


In this case I want the current time so I rendered the date string. The second variable is the "format" that I want the date/time rendered and finally the timeZone which is correct "America/Denver";

I did a var_dump of all variables when the problem was happening and all three were arriving to the function as expected.

There are cases where I don't want the current time. For example I want to find out when something was done in the past but based on that specific time zone.

So that is why I made a function that does both but I didn't see any issues with the way I set it up until now where i'm having some major issues.

This is really weird and i'm not sure what is going on. Any help would be greatly appreciated!

Regards,

Nick

nickCR

2:46 am on Apr 16, 2010 (gmt 0)

10+ Year Member



Well I decided to add another value to the end which is "$dateProvided = false"

So I found the calls to this function that were providing a date in the past and added true as the fourth value.

Then I updated the function to this:


function convert_tz ($date_str, $format, $tz, $dateProvided = false) {

if($dateProvided){

$time = strtotime($date_str);
putenv("TZ={$tz}");
$ret = date($format, $time);
putenv("TZ=America/New_York");

} else {

putenv("TZ={$tz}");
$ret = date($format);
putenv("TZ=America/New_York");

}

return $ret;

}


Still would love to know why the strtotime is going nuts at certain times of the day but anyway.

astupidname

3:01 am on Apr 16, 2010 (gmt 0)

10+ Year Member



The problem is most likely with your formatting of the date. Lower case 'h' denotes hours in 12 hour format, as opposed to upper case 'H' denoting hours in 24 hour format. Then the real kicker is that lower case 'm' is for "A numeric representation of a month (from 01 to 12)", which you have mixed in to the minutes portion of the date format, which should be 'i'.
Brush up on your date formatting here. [w3schools.com]

nickCR

5:22 am on Apr 16, 2010 (gmt 0)

10+ Year Member



I feel like a retard...

Thank you for pointing out the problem. Now I know why I was having this problem.

Matthew1980

7:12 am on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there NickCR,

Well I'm glad as you got it sorted in the end, I had a feeling as it was to do with the formatting. Handy to print off the table from php.net that shows the format char's and what they stand for.

And we all make errors from time to time, thats just human nature ;-p

Cheers,
MRb

nickCR

3:43 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



Yeah, sometimes when your coding your typing so fast your mind thinks "hours = h, minutes = m and seconds = s"....

I checked all the other functions and the date format input was fine so in this case that was the problem. I'm really happy that it was a problem on my end to be honest.

Hopefully I don't make this mistake again, considering all the trouble it caused me it probably won't.

g1smd

7:46 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do yourself a favour and test the code for times throughout the entire 24 hours, in 15 or 30 minute increments. Also test both near midnight local time and midnight UTC at month end, rolling over into the following month.

I've found code where time "goes backwards" for part of the day as the UTC offset was being subtracted instead of added (or vice versa).