Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How to reverse engineer the day of the year 3212 (YDDD) into 2013-08-01 (YYYY-mm-dd)

Tags:

date

php

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

like image 611
zzlalani Avatar asked Aug 01 '13 06:08

zzlalani


People also ask

How to convert date format YYYY-MM-DD to DD-MM-YYYY in PHP?

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.

How can I get current date in YYYY-MM-DD format in PHP?

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>".

How to use date format in PHP?

Example. $date=date_create("2013-03-15"); echo date_format($date,"Y/m/d H:i:s");


1 Answers

$date = "3212";
echo DateTime::createFromFormat("Yz", "201$date")->format("Y-m-d");
// 2013-08-01
  • DateTime::createFromFormat()
  • See it running online
like image 98
salathe Avatar answered Nov 04 '22 10:11

salathe