Get year from date php
I have a date in this format Show
wattostudios 8,59613 gold badges42 silver badges57 bronze badges asked Dec 25, 2010 at 7:44
Shakti SinghShakti Singh 82.1k20 gold badges132 silver badges150 bronze badges 1
The DateTime class does not use an unix timestamp internally, so it han handle dates before 1970 or after 2038. answered Dec 25, 2010 at 7:57
0 You can use the
Note however that PHP can handle year upto 2038 You can test it out here If your date is always in that format, you can also get the year like this:
answered Dec 25, 2010 at 7:47
SarfrazSarfraz 371k74 gold badges529 silver badges575 bronze badges 6 I would use this:
It appears the date is coming from a source where it is always the same, much quicker this way using explode.
CuberChase 4,4205 gold badges32 silver badges52 bronze badges answered Jul 10, 2013 at 23:09
carninicarnini 651 silver badge7 bronze badges You can try strtotime() and date() functions for output in minimum code and using standard way.
answered Jun 4, 2020 at 11:37
ShwetaShweta 5515 silver badges11 bronze badges
answered Jan 21, 2014 at 13:13
HibaHiba 1011 silver badge2 bronze badges
answered Dec 25, 2010 at 8:17
fvoxfvox 1636 bronze badges You can achieve your goal by using php date() & explode() functions:
That is it. Happy coding :) answered Feb 12, 2020 at 6:08
Rashed RahatRashed Rahat 2,0501 gold badge17 silver badges32 bronze badges Assuming you have the date as a string (sorry it was unclear from your question if that is the case) could split the string on the - characters like so:
answered Dec 25, 2010 at 7:49
dshipperdshipper 3,4193 gold badges29 silver badges41 bronze badges 1
You can use explode also answered Nov 12, 2012 at 9:39
V A SV A S 3,2094 gold badges33 silver badges38 bronze badges You wrote that format can change from YYYY-mm-dd to dd-mm-YYYY you can try to find year there
answered Jul 10, 2013 at 23:32
bidonbidon 32 bronze badges How to get the year of a date in PHP?$date = DateTime::createFromFormat("Y-m-d", "2068-06-15"); echo $date->format("Y");
How to get year from DateTime object in PHP?Method 1: Using only date() function to get current year
It can take two parameters. If you use only one parameter, It will return info about the current time. echo "4 digit of current year is: " . date ( "Y" );
How can get current year start and end date in PHP?$year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);
How to get current date in PHP With time?You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.
|