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

x
?
Solved

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

Posted on 2008-06-14
7
Medium Priority
?
3,333 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 400 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 200 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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