I have a date in YDDD
format such 3212
I want to convert this date into default date string i.e. 2013-08-01 in PHP
Since the first value Y
is the only character for Year, so I've decided to take the first three characters from the current Year i.e. 201 from 2013
The following is the code I've written for year
<?php
$date = "3212"
$y = substr($date,0,1); // will take out 3 out of year 3212
$ddd = substr($date,1,3); // will take out 212 out of year 3212
$year = substr(date("Y"),0,3) . $y; //well create year "2013"
?>
Now How can I use $year
and 212
to convert it into 2013-08-01 using PHP
EDIT
FYI: My PHP Version is 5.3.6
Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.
date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y"). "<br />"; echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />"; echo date("m/d/Y")."<br />"; echo "<strong>Display current date mm-dd-yyyy format </strong>".
Example. $date=date_create("2013-03-15"); echo date_format($date,"Y/m/d H:i:s");
$date = "3212";
echo DateTime::createFromFormat("Yz", "201$date")->format("Y-m-d");
// 2013-08-01
DateTime::createFromFormat()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With