date parse

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
ginoitalo
Forum Newbie
Posts: 4
Joined: Fri Aug 29, 2003 1:55 pm

date parse

Post 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 ?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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;
}
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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).
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Can't say I've used strtotime much so not sure what's throwing it.

Looks like mktime() then.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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";
?>
ginoitalo
Forum Newbie
Posts: 4
Joined: Fri Aug 29, 2003 1:55 pm

Post 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
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

Post Reply