On 23/06/11 12:30 AM, Richard Quadling wrote:
On 22 June 2011 15:05, David Nicholls<dcn@xxxxxxxxxxxxxx> wrote:
I'm trying to convert a date and time string using strtotime()
The date and time strings are the first entry in each line in a csv file in
the form:
22/06/2011 9:47:20 PM, data1, data2,...
I've been trying to use the following approach, without success:
function read_data($filename)
{
$f = fopen($filename, 'r');
while ($d = fgetcsv($f)) {
$format = 'd/m/Y h:i:s';
$dt = DateTime::createFromFormat($format, $d[0]);
$data[] = array(strtotime($dt), $d[1]); //convert date/time
}
fclose($f);
return $data;
}
Obviously I'm not getting the $format line right, as the resulting $dt
values are empty. (I have previously used this reading process successfully
with better behaved date and time strings).
Advice appreciated.
DN
<?php
$data = '22/06/2011 9:47:20 PM';
$format = 'd/m/Y g:i:s A';
$datetime = DateTime::createFromFormat($format, $data);
echo date('r', $datetime->getTimestamp());
?>
outputs ...
Wed, 22 Jun 2011 21:47:20 +0100
Yes, partly works, but I'm now getting:
Wed, 22 Jun 2011 20:19:56 +1000
Warning: strtotime() expects parameter 1 to be string, object given in
<xxx>.php on line 12
in:
5 function read_data($filename)
6 {
7 $f = fopen($filename, 'r');
8 while ($d = fgetcsv($f)) {
9 $format = 'd/m/Y g:i:s A';
10 $dt = DateTime::createFromFormat($format, $d[0]);
11 echo date('r', $dt->getTimestamp());
12 $data[] = array(strtotime($dt), $d[1]); //convert date/time
13 }
14 fclose($f);
15 return $data;
15 }
So $dt is apparently not a string?
DN
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php