Solved

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

Posted on 2008-06-14
7
3,323 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
Comment Utility
$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
Comment Utility
"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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Expert Comment

by:LordOfPorts
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your help!
0
 

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress syntax error 1 26
mysql Encryption with PHP 8 44
Time difference 10 32
how can I dected if URL has variable? 7 19
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now