-2

I have a variable in php:

$d = "19:02:13 Nov 07, 2010 PST";

What is the quickest way to convert the format so I get (mysql DATETIME):

$d = "2010-11-07 19:02:13";
Yeti
  • 5,628
  • 9
  • 45
  • 71
  • 4
    [How many more date formatting questions does this site need?](http://stackoverflow.com/search?q=convert+date+format+[php]) [Use the search function before asking superfluous duplicates.](http://stackoverflow.com/questions/ask-advice) – Gordon Jan 06 '11 at 12:48
  • possible duplicate of [PHP convert one date into another date format](http://stackoverflow.com/questions/2167916/php-convert-one-date-into-another-date-format) – Gordon Jan 06 '11 at 12:49
  • @Gordon the number is infinite, by design. More repetitive questions (lame ones preferably, as them take more answers) -> more answers -> more views -> more ads shown -> more profit. Insisting on using search you're cutting down site revenues! And, honestly, it's totally in vain. The answers being written way faster than you write your comment :) – Your Common Sense Jan 06 '11 at 13:13
  • @Col while I like your cynicism in that comment, this being by design does not match with what is officially suggested in [Ask Advice](http://stackoverflow.com/questions/ask-advice). As for answers being written faster than my comments, well, dont underestimate my bookmarklet foo. It took me longer for this one, but I can provide certain standard remarks with a click of a button ;) – Gordon Jan 06 '11 at 13:20

2 Answers2

3

try this

date_default_timezone_set("America/Los_Angeles"); 
echo date("Y-m-d H:i:s", strtotime("19:02:13 Nov 07, 2010 PST"));
Xavier Barbosa
  • 3,919
  • 1
  • 20
  • 18
  • 1
    Which doesn't work;) Returns 2010-11-08 04:02:13 when I run it because it doesn't take the timesone into calculation. Like mentioned by middaparka setting the timezone will solve it. – Sondre Jan 06 '11 at 12:48
  • gnagna :) with the timezone setted – Xavier Barbosa Jan 06 '11 at 12:58
1

As easy (though not epically efficient) way would be to use strtotime as follows:

date('Y-m-d H:i:s', strtotime('19:02:13 Nov 07, 2010 PST'))

However, make sure you've set the correct local timezone via date_default_timezone_set or this will not work correctly.

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • 1
    What is the issue of efficiency regarding `strtotime()`? I've used it consistently in my applications, but have never bothered to benchmark or look into alternatives. – Dan Lugg Jan 06 '11 at 12:49
  • @TomcatExodus Purely based on the way it (somewhat inevitably) has to analyse the source string it's not something I'd tend to use too often. That said, it's nothing to worry about unless you're using it excessively. – John Parker Jan 06 '11 at 12:53
  • **@middaparka**; Yes, when used it's often only to reformat dates coming from an external source with different formatting, few per request, often for MySQL insertion. Regardless, what alternatives would you consider as more efficient? I'd immediately assume Regex is out. – Dan Lugg Jan 06 '11 at 12:56
  • 1
    @TomcatExodus I've have thought that if you know the incoming format then tokenising or exploding the string would be more efficient. That said, there's also the new date_parse_from_format function in 5.3 that might be more efficient. However, it should be noted that this is an assumption on my part - I've not carried out any testing to confirm this suspicion. – John Parker Jan 06 '11 at 13:28
  • 2
    @TomCatExodus Seriously, before you go looking for alternatives to `strtotime` (or anything ftm), find proof that it is indeed slowing down your application so much that changing it does make sense. Profile your application, look at the numbers and then go figure. Chances are, you'll find better candidates for optimizing at completely different portions of your code. – Gordon Jan 06 '11 at 13:30
  • 1
    @Gordon - Totally agree. (That's the last time I make a throwaway comment about something being "not epically efficient".) :-) – John Parker Jan 06 '11 at 13:32
  • **@Gordon**; Certainly, I've been guilty of micro-optimizing in the wrong places before, I just wanted clarification from **middaparka** having stated it was potentially inefficient. – Dan Lugg Jan 06 '11 at 13:34