Solved

PHP: How to convert dd/mm/yyyy to yyyy-mm-dd in PHP?

Posted on 2008-06-14
7
3,332 Views
Last Modified: 2013-12-13
PHP


Hello,

I have string that looks like "dd/mm/yyyy blah blah". I wish to change "dd/mm/yyyy"  to "yyyy-mm-dd", so that it looks like "yyyy-mm-dd blah blah".

How do I do this in PHP script?


Thanks.
0
Comment
Question by:gingera
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 100 total points
ID: 21786411
$str = "29/05/2008";
$str  = explode("/",$str);
$str = $str[2] . "-" . $str[1] . "-" . $str[0];

echo $str;
0
 

Assisted Solution

by:zinosoufi
zinosoufi earned 50 total points
ID: 21786444
"dd/mm/yyyy"  to "yyyy-mm-dd

Try this ($original is the date in the "dd/mm/yyty" format) :

function convertdate($original)
{
$day = substr($original,0,2);
$month =substr($orginal,3,2);
$year = substr($original,6,4);

$newdate = "$year-$month-$day";

return $newdate;
}
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 100 total points
ID: 21786474
If you want it in a function, then use:
function formatDate( $str ) 
$str  = explode("/",$str);
$str = $str[2] . "-" . $str[1] . "-" . $str[0]; 
return $str;
} 
echo formatDate( "29/05/2008");

Open in new window

0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 19

Expert Comment

by:LordOfPorts
ID: 21786510
Assuming your string also contains the "blah blah" section below is one example of a functioning solution:
<?php
 
// Original string value
$sOriginal = "dd/mm/yyyy blah blah";
 
// Display the original value
echo '<span style="background-color: #FFFF00;">ORIGINAL:</span> <strong>'.$sOriginal.'</strong><br />';
 
// Separate the date part from the "blah blah" part"
$sDate = substr($sOriginal, 0, strpos($sOriginal, ' '));
 
// Separate the "blah blah" part from the date
$sRest = substr($sOriginal, strpos($sOriginal, ' '), strlen($sOriginal));
 
// Separate the date into day, month, and year parts
$sDate = split('/', $sDate);
 
// Construct the new string
$sNew = $sDate[2].'-'.$sDate[0].'-'.$sDate[1].$sRest;
 
// Display the new string
echo '<span style="background-color: #FFFF00;">NEW:</span> <strong>'.$sNew.'</strong><br />';
 
?>

Open in new window

DateSplit.png
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 350 total points
ID: 21786527
Sorry, correction, month comes before day in the ISO date format:
<?php
 
// Original string value
$sOriginal = "dd/mm/yyyy blah blah";
 
// Display the original value
echo '<span style="background-color: #FFFF00;">ORIGINAL:</span> <strong>'.$sOriginal.'</strong><br />';
 
// Separate the date part from the "blah blah" part"
$sDate = substr($sOriginal, 0, strpos($sOriginal, ' '));
 
// Separate the "blah blah" part from the date
$sRest = substr($sOriginal, strpos($sOriginal, ' '), strlen($sOriginal));
 
// Separate the date into day, month, and year parts
$sDate = split('/', $sDate);
 
// Construct the new string
$sNew = $sDate[2].'-'.$sDate[1].'-'.$sDate[0].$sRest;
 
// Display the new string
echo '<span style="background-color: #FFFF00;">NEW:</span> <strong>'.$sNew.'</strong><br />';
 
?>

Open in new window

DateSplit.png
0
 

Author Closing Comment

by:gingera
ID: 31467232
Thanks for your help!
0
 

Author Comment

by:gingera
ID: 21840468
LordOfPorts' suggested solution is what I am after, because my field contains blah blah blah after the date. Thanks.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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