Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parsing a Date Time String smartly

Tags:

php

I'm trying to parse a date time string combination to Date and Time and wanted to know if there a smarter way

Sample Data

9/3/2013 8:50:05 AM
9/4/2013 1:42:28 PM
9/11/2013 12:01:21 PM
....    

Here's what I'm doing..Looking thru the list of Date Time string

   <?php
   $x = "9/3/2013 8:50:05 AM"
   $ExDt = str_getcsv(trim($x," ");
   $ExDate = $ExDt[0];
   $ExTm = $ExDt[1] . " " . $ExDt[2];
   $ExTime = date("H:i:s", strtotime($ExTm)); // Change to 24 hour format

   echo $ExDate;
   echo $ExTime;

   ?>
like image 981
Tim B Avatar asked Sep 25 '13 05:09

Tim B


1 Answers

I feel safer converting with this (timezone not taken into account in the example)

$start = '25/12/2013 10:13:46';
$new_date = DateTime::createFromFormat('d/m/Y H:i:s', $start);
echo $new_date->format('Y-m-d H:i:s');
like image 160
Aaron Gong Avatar answered Oct 11 '22 14:10

Aaron Gong