Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
121 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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 111

Accepted Solution

by:
Ray Paseur earned 1600 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 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 400 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

Technology Partners: 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!

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

730 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