Solved

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

Posted on 2008-06-14
7
3,330 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 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 …

739 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