Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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";

Open in new window

Avatar of Eduardo Fuerte

ASKER

Unfortunatelly I don't have too much time just now and the problem I have is:

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;			

Open in new window



Produces:

User generated image
Could you point  a workaround?
Ray

Sorry, our message crossed.

By using your example, just do:

$value = str_replace('/', '-', $value);

Open in new window


It looks ok!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for help