PHP How to deploy day of the year 3212 (YDDD) on 2013-08-01 (YYYY-mm-dd)

I have a date in YDDD format such as 3212
I want to convert this date to a default date string, i.e. 2013-08-01 in PHP
Since the first Y value is the only character for Year, so I decided to take the first three characters from the current year i.e. 201 from 2013

Below is the code I wrote during the 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 to use $year and 212 to convert it to 2013-08-01 using PHP

EDIT
FYI: My PHP Version 5.3.6

+7
date php
source share
4 answers
 $date = "3212"; echo DateTime::createFromFormat("Yz", "201$date")->format("Ymd"); // 2013-08-01 
+13
source share
 $yddd = 3212; preg_match('/^(\d)(\d{3})$/', $yddd, $m); echo date('Ym-d', strtotime("201{$m[1]}-01-01 00:00:00 +{$m[2]} days")); 
+8
source share

If you use code in PHP 5.3 or later, you can convert $ year and $ ddd to a useful date using date_create_from_format. For example:

 $date = date_create_from_format("Yz", "$year-$ddd"); echo date_format($date, "Ymd"); 
+6
source share
 // Formatted Date (YDDD) $dateGiven = "3212"; // Generate the year based on the first digit $year = substr(date("Y"),0,3).substr($dateGiven,0,1); // Split out the day of the year $dayOfTheYear = substr($dateGiven,1,3); // Create a date object from the formatted date $date = DateTime::createFromFormat('z Y', "{$dayOfTheYear} {$year}"); // Output the date in the desired format echo $date->format('Ym-d'); 

View it here: https://eval.in/private/69daafa849ee36

+2
source share

All Articles