Solved

Could you point the way to convert a date from 'DD/MM/YYYY HH:mm:SS'  to  'YYYY-DD-MM HH:mm:SS'  ?

Posted on 2016-10-19
7
109 Views
Last Modified: 2016-10-19
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.
0
Comment
Question by:Eduardo Fuerte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41850110
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41850134
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

0
 

Author Comment

by:Eduardo Fuerte
ID: 41850142
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:

img001
Could you point  a workaround?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Eduardo Fuerte
ID: 41850159
Ray

Sorry, our message crossed.

By using your example, just do:

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

Open in new window


It looks ok!
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 41850168
Yes, there is different meaning in the slash and the dash.  The slash assumes the American way of writing dates.  The dash is what you want with your date format.

Best regards, ~Ray
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
ID: 41850179
You can use the createFromTime method of the DateTime object. Basically, you tell the function what your format is and it will create a date based on that. You can then output the result using the format method to get your new format:

$date = "19/10/2016 14:48:21";
$origFormat = "d/m/Y H:i:s"; // DD/MM/YYYY HH:MM:SS
$newFormat = "Y-d-m H:i:s"; // YYYY-DD-MM HH:MM:SS

$newDate = DateTime::createFromFormat($origFormat, $date);
echo $date->format($newFormat);

Open in new window

3
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41850346
Thanks for help
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question