6

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;

   ?>
5
  • 1
    php.net/manual/en/datetime.createfromformat.php Commented Sep 25, 2013 at 5:03
  • strtotime("9/3/2013 8:50:05 AM") doesnt simply work? Commented Sep 25, 2013 at 5:08
  • So the only thing you're changing here is to turn the time into 24-hour? Commented Sep 25, 2013 at 5:11
  • What format are your date strings, ie m/d/Y or d/m/Y? Commented Sep 25, 2013 at 6:10
  • Thanks for your response. strtotime("9/3/2013 8:50:05 AM") did not work for me. Commented Sep 25, 2013 at 14:34

4 Answers 4

7

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');
Sign up to request clarification or add additional context in comments.

1 Comment

Afaik. the createFromFormat converts only 09/04/2013 01:42:28, but not 9/4/2013 1:42:28. At least it did not work by me. :S
2
$dt = new DateTime('9/3/2013 8:50:05 AM');
echo $dt->format('m/d/Y');
echo $dt->format('H:i:s');

8 Comments

I'd still use createFromFormat as that date could be March 9 or September 3.
@Phil, Please see php.net/manual/en/datetime.formats.date.php Those would still be parsed correctly when using /
@Rob I think you misunderstand. In non-US locales (such as Aus), 9/3/2013 is Sat, 09 Mar 2013. The DateTime constructor however will interpret that date as Tue, 03 Sep 2013. Using DateTime::createFromFormat allows the developer to remove the ambiguity
@Rob The point is, we don't know what locale TimB is catering for (all the dates in the question are ambiguous). Using createFromFormat instead of the DateTime constructor allows a developer flexibility. The alternative is to change the input format to an ISO date (YYYY-mm-dd) or use hyphens instead of slashes for non-US date formats (which nobody actually uses). Changing input formats, especially from a CSV file seems much harder to me.
The dump is from a card reader and comes in only one date time format as shown above. $dt = new DateTime('9/3/2013 8:50:05 AM'); Did Not work.
|
1
$time = strtotime('9/3/2013 8:50:05 AM');
echo date('H:i:s',$time); // 08:50:05
echo date('Y-m-d',$time); // 2013-03-09

Comments

0
<?php
$x = "9/3/2013 8:50:05 AM";
$time = strtotime($x);
echo date('m/d/Y', $time), "\n";
echo date('H:i:s', $time), "\n";

See the result here

read more about date time format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.