Page 1 of 1
date parse
Posted: Fri Aug 29, 2003 6:22 pm
by ginoitalo
Code: Select all
<?php
$paydate = "18:30:30 Jan 1, 2000 PST";
$dt = date('H:i:s M j, Y T', $paydate);
print date('M-j-Y H:i:s T', $dt);
?>
Does NOT print the correct date I passed in, rather
'Dec-31-1969 19:00:19 Eastern Standard Time'
anyone know why or how I can fix this ?
Posted: Fri Aug 29, 2003 6:49 pm
by JAM
Not solving it, but T is timezone for the machine (server), so if you and the server are at different locations, you might get that (or if the $paydate in your case is something thats just a manufactured string).
Meaning, if you are trying to convert something to a date() using T, it will take the value from the machine it's running on...
Might help?:
Code: Select all
// stolen from php.net comments...
/*
$current should be the timezone on the
current machine, $target is the timezone
you want to calculate. Both should be in
hours. For example, GMT -05:00 should
be -5. GMT +12:00 should be 12. You can
use the returned timestamp with the date
function.
ex: echo date('r', zonechange(-5, 10));
would echo the date for a +10:00
timezone on a machine in -05:00.
*/
function zonechange ($current, $target) {
$current = -1 * $current;
$zonedate = mktime(date('G'), date('i'), date('s'), date('n'),
date('j'), date('Y'), 1) + (($current + $target) * 3600);
return $zonedate;
}
Posted: Fri Aug 29, 2003 7:03 pm
by McGruff
The date() fn takes a unix timestamp as an argument. From the manual:
To generate a timestamp from a string representation of the date, you may be able to use strtotime(). Additionally, some databases have functions to convert their date formats into timestamps (such as MySQL's UNIX_TIMESTAMP function).
Posted: Fri Aug 29, 2003 7:09 pm
by JAM
Yes, I had that thought to, but the following didn't work as expected:
Code: Select all
<pre>
<?php
$paydate = "18:30:30 Jan 1, 2000 PST";
echo $paydate."\n";
$foo = strtotime($paydate);
echo $foo."\n";
$bar = date('H:i:s M j, Y T', $foo);
echo $bar."\n";
?>
// 18:30:30 Jan 1, 2000 PST
// 946780230
// 03:30:30 Jan 2, 2000 W. Europe Standard Time
Posted: Fri Aug 29, 2003 7:18 pm
by McGruff
Can't say I've used strtotime much so not sure what's throwing it.
Looks like mktime() then.
Posted: Fri Aug 29, 2003 7:37 pm
by JAM
Ahhh...
Using the putenv, changing the timezone settings, you might be able to work around it...
Code: Select all
<pre>
<?php
putenv ("TZ=PST8PST");
$one = "18:30:30 Jan 1, 2000 PST";
$one = strtotime($one);
$one = date('H:i:s M j, Y T', $one);
echo 'One: '.$one."\n";
?>
Posted: Fri Aug 29, 2003 11:40 pm
by ginoitalo
JAM, thanks that worked great
I needed this to insert the string into a mySql DATETIME field,
which I don't think takes the PST or EST
cool workaround
Posted: Sat Aug 30, 2003 2:11 pm
by macewan