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
77 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
  • 3
  • 3
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Eduardo Fuerte
Comment Utility
Ray

Sorry, our message crossed.

By using your example, just do:

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

Open in new window


It looks ok!
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
Comment Utility
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
Comment Utility
Thanks for help
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now