Solved

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

Posted on 2008-06-14
7
3,326 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…

828 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