Viewed   119 times

With a date string of Apr 30, 2010, how can I parse the string into 2010-04-30 using PHP?

 Answers

4

Try http://php.net/manual/en/function.strtotime.php to convert to a timestamp and then http://www.php.net/manual/en/function.date.php to get it in your own format.

Wednesday, October 19, 2022
3
std::tm tm = {};
std::stringstream ss("Jan 9 2014 12:35:34");
ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));

GCC prior to version 5 doesn't implement std::get_time. You should also be able to write:

std::tm tm = {};
strptime("Thu Jan 9 2014 12:35:34", "%a %b %d %Y %H:%M:%S", &tm);
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
Thursday, September 22, 2022
 
2
$ll = '[{"lat":37.790388261934424,"lng":-122.46047996826172},{"lat":37.789608231530124,"lng":-122.46344112701416}]';
$ll = json_decode($ll);
print_r($ll);

Prints...

Array
(
    [0] => stdClass Object
        (
            [lat] => 37.7903882619
            [lng] => -122.460479968
        )

    [1] => stdClass Object
        (
            [lat] => 37.7896082315
            [lng] => -122.463441127
        )

)
Monday, November 21, 2022
5

I think you want to use the HH format, rather than 'hh' so that you are using hours between 00-23. 'hh' takes the format in 12 hour increments, and so it assumes it is in the AM.

So this

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2009-08-19 12:00:00");
System.out.print(date.toString());

Should print out

Wed Aug 19 12:00:00 EDT 2009

Sunday, November 13, 2022
 
4

explode will do the trick for that:

$pieces = explode("/", $date);
$d = $pieces[1];
$m = $pieces[0];
$y = $pieces[2];

Alternatively, you could do it in one line (see comments - thanks Lucky):

list($m, $d, $y) = explode("/", $date);
Sunday, December 18, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :