Eduardo Fuerte
asked on
Could you point the way to convert a date from 'DD/MM/YYYY HH:mm:SS' to 'YYYY-DD-MM HH:mm:SS' ?
Hi Experts
Could you point the way to convert a date from 'DD/MM/YYYY HH:mm:SS' to 'YYYY-DD-MM HH:mm:SS' ?
Thanks in advance.
Could you point the way to convert a date from 'DD/MM/YYYY HH:mm:SS' to 'YYYY-DD-MM HH:mm:SS' ?
Thanks in advance.
Here's an example.
<?php // demo/temp_efuerte.php
/**
* https://www.experts-exchange.com/questions/28977419/Could-you-point-the-way-to-convert-a-date-from-'DD-MM-YYYY-HH-mm-SS'-to-'YYYY-DD-MM-HH-mm-SS'.html
*
* https://www.experts-exchange.com/questions/28977154/Function-to-convert-string-date-into-a-sql-format-date-string.html
* https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
*/
error_reporting(E_ALL);
function DateThing($s, $p='c')
{
$ts = strtotime($s);
if (!$ts)
{
trigger_error("Date $s is not valid", E_USER_WARNING);
return 'INVALID';
}
return date($p, $ts);
}
// SHOW THAT THE ORIGINAL DOES NOT WORK
$old = '19/10/2016 09:36:00';
$pat = 'Y-m-d H:i:s';
$new = datething($old, $pat);
echo PHP_EOL . "$old => $new";
echo '<br>' . PHP_EOL;
// MODIFY THE ORIGINAL TO MAKE IT WORK
$old = str_replace('/', '-', $old);
$new = datething($old, $pat);
echo PHP_EOL . "$old => $new";
ASKER
Unfortunatelly I don't have too much time just now and the problem I have is:
Produces:
Could you point a workaround?
case 'DD/MM/YYYY HH:mm:SS':
switch ($to) {
case 'YYYY-DD-MM HH:mm:SS':
var_dump($value);
$var1 = strtotime($value);
var_dump($var1);
$aux = date('Y-m-d H:i:s', strtotime($value));
var_dump($aux);
die;
Produces:
Could you point a workaround?
ASKER
Ray
Sorry, our message crossed.
By using your example, just do:
It looks ok!
Sorry, our message crossed.
By using your example, just do:
$value = str_replace('/', '-', $value);
It looks ok!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for help
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
Recommend you look closely at the use of slashes vs dashes in the source date string.