[PHP] newbie date time question
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 General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] newbie date time question
On 23/06/11 12:23 AM, Adam Balogh wrote: hi, you have a PM(/AM) in your date ($d[0]), so put an A (Uppercase Ante meridiem and Post meridiem) format char to your $format variable. b3ha Thanks, Adam. Tried that and it's now throwing an error: Catchable fatal error: Object of class DateTime could not be converted to string in xxx.php on line 12 Line 12 follows the attempt at conversion 10 $format = 'd/m/Y h:i:s A'; 11 $dt = DateTime::createFromFormat($format, $d[0]); 12 echo $dt,...; DN -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] newbie date time question
On 23/06/11 12:30 AM, Richard Quadling wrote: On 22 June 2011 15:05, David Nichollsd...@dcnicholls.com 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} 14fclose($f); 15return $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
Re: [PHP] newbie date time question
OK, looks like I have fixed problem, using: $format = 'd/m/Y g:i:s A'; $dt = date_create_from_format($format, $d[0]); $dt2 = date('r', $dt-getTimestamp()); $data[] = array(strtotime($dt2), $d[1]); Not elegant but it gives me the date/time value. Thanks, Adam and Richard DN On 23/06/11 12:51 AM, Adam Balogh wrote: $dt is an object as the error says, so you cant echo it, becouse its not a string (it can be with __toString magic method, or use print_r/var_dump to output your object). Try the format ( http://www.php.net/manual/en/datetime.format.php) method on your $dt object. On Wed, Jun 22, 2011 at 4:41 PM, David Nichollsd...@dcnicholls.com wrote: On 23/06/11 12:23 AM, Adam Balogh wrote: hi, you have a PM(/AM) in your date ($d[0]), so put an A (Uppercase Ante meridiem and Post meridiem) format char to your $format variable. b3ha Thanks, Adam. Tried that and it's now throwing an error: Catchable fatal error: Object of class DateTime could not be converted to string inxxx.php on line 12 Line 12 follows the attempt at conversion 10 $format = 'd/m/Y h:i:s A'; 11 $dt = DateTime::createFromFormat($**format, $d[0]); 12 echo $dt,...; DN -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: newbie date time question
On 23/06/11 1:35 AM, Shawn McKenzie wrote: On 06/22/2011 09:05 AM, David Nicholls 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 I'm late to the party, but strtotime works great, though you need to give it what it expects: $ts = strtotime(str_replace('/', '-', $date)); Thanks, Shawn, that's a bit more elegant! I'll give it a go. I didn't know how to do the str_relace bit. Thanks DN -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] newbie date time question
On 23/06/11 1:02 AM, Stuart Dallas wrote: On Wednesday, 22 June 2011 at 15:59, David Nicholls wrote: OK, looks like I have fixed problem, using: $format = 'd/m/Y g:i:s A'; $dt = date_create_from_format($format, $d[0]); $dt2 = date('r', $dt-getTimestamp()); $data[] = array(strtotime($dt2), $d[1]); Not elegant but it gives me the date/time value. $dt-getTimestamp() will give you the same value as strtotime($dt2), so you don't need the inelegance. $format = 'd/m/Y g:i:s A'; $dt = date_create_from_format($format, $d[0]); $data[] = array($dt-getTimestamp(), $d[1]); -Stuart Thanks, Stuart. That's much prettier! DN -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: newbie date time question
On 23/06/11 10:04 AM, Shawn McKenzie wrote: On 06/22/2011 06:54 PM, David Nicholls wrote: I'm late to the party, but strtotime works great, though you need to give it what it expects: $ts = strtotime(str_replace('/', '-', $date)); Thanks, Shawn, that's a bit more elegant! I'll give it a go. I didn't know how to do the str_relace bit. Thanks The deal is that if you use the / then strtotime interprets it as m/d/y and if you use - it interprets it as d-m-y. Yes, I knew the '-' format would work, I just didn't know quite how to convert the '/' to '-'. A very simple and clean solution. The final form is: function read_data($filename) { $f = fopen($filename, 'r'); while ($d = fgetcsv($f)) { $ts = strtotime(str_replace('/','-',$d[0])); $data[] = array($ts, $d[1]); } fclose($f); return $data; } DN -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php