Solved

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

Posted on 2008-06-14
7
3,329 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
Independent Software Vendors: 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!

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

756 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