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
117 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: 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

627 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