Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the year from specified date php

Tags:

date

php

I have a date in this format 2068-06-15. I want to get the year from the date, using php functions. Could someone please suggest how this could be done.

like image 842
Shakti Singh Avatar asked Dec 25 '10 07:12

Shakti Singh


People also ask

How to extract year from a date in PHP?

$date = DateTime::createFromFormat("Y-m-d", "2068-06-15"); echo $date->format("Y");

How to get year from string in PHP?

@scoota269 is right - and you can test this using echo date("Y-m-d", $oldDateUnix) . In this case, it even bugs up and results (1970-01-01) which is unix time 0.

How can I get the day of a specific date with PHP?

You can use the date function. I'm using strtotime to get the timestamp to that day ; there are other solutions, like mktime , for instance.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


1 Answers

$date = DateTime::createFromFormat("Y-m-d", "2068-06-15"); echo $date->format("Y"); 

The DateTime class does not use an unix timestamp internally, so it han handle dates before 1970 or after 2038.

like image 196
Maerlyn Avatar answered Oct 18 '22 01:10

Maerlyn