Get the year from specified date php

php date
strtotime
php datetime
string to date php
php get year from datetime
timestamp to date php
php get month from date
php get year from date object

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.

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

Get the year from specified date php, How to get year and month from a given date. e.g. $dateValue = '2012-01-05';. From this date I need to get year as 2012 and month as January. The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

You can use the strtotime and date functions like this:

echo date('Y', strtotime('2068-06-15'));

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:

$parts = explode('-', '2068-06-15');
echo $parts[0];

How to get year and month from a date - PHP, Returns a string formatted according to the given format string using the given It is possible to use date() and mktime() together to find dates in the future or the past echo date("oW", strtotime("2011-01-01")); // gives 201052 (Year is different � Version Description; 5.1.1: There are useful constants of standard date/time formats that can be used to specify the format parameter.: 5.1.0: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT.

I would use this:

$parts = explode('-', '2068-06-15');
echo $parts[0];

It appears the date is coming from a source where it is always the same, much quicker this way using explode.

date - Manual, The PHP date() function is used to format a date and/or a time. of characters, denoting the date and/or time at which a certain event occurred. Get a Date Y - Represents a year (in four digits); l (lowercase 'L') - Represents the day of the� There are few ways you can retrieve year from a date. In the following you’ll see different ways to retrieve year from current date or from any date – Method 1: Using only date() function to get current year. PHP’s date() function can let you know date and time related information based on the formatting string it takes in its first

  public function getYear($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("Y");
}

public function getMonth($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("m");
}

public function getDay($pdate) {
    $date = DateTime::createFromFormat("Y-m-d", $pdate);
    return $date->format("d");
}

PHP Date and Time, n - A numeric representation of a month, without leading zeros (1 to 12); t - The number of days in the given month; L - Whether it's a leap year (1 if it is a leap year,� DATE_RFC1123 - RFC 1123 (example: Fri, 12 Apr 2013 15:52:01 +0000) DATE_RFC2822 - RFC 2822 (Fri, 12 Apr 2013 15:52:01 +0000) DATE_RFC3339 - Same as DATE_ATOM (since PHP 5.1.3) DATE_RSS - RSS (Fri, 12 Aug 2013 15:52:01 +0000) DATE_W3C - World Wide Web Consortium (example: 2013-04-12T15:52:01+00:00) timestamp: Optional. Specifies an integer Unix

<?php
list($year) = explode("-", "2068-06-15");
echo $year;
?>

PHP date() Function, I'm new to PHP and I am hoping that some here can help me further with date formatting in PHP. I have a range of dates that sometimes are as follows:� YEAR() function. MySQL YEAR() returns the year for a given date. The return value is in the range of 1000 to 9999 or 0 for 'zero' date. Syntax: YEAR(dt)

Extracting Year, or Year/Month, or Full Date - PHP, Before you get too advanced using date functions, be sure also to see the calendar <?php /* Code to show a message only for a certain time frame. This is a simple <?php //function like dateDiff Microsoft //not error in year Bissesto function� PHP Forms PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete PHP Advanced PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions PHP OOP

Date/Time Functions - Manual, Subtracting years from a given date using the strtotime and date functions. If you have a specific date and you want to subtract a year from it,� Definition and Usage. The YEAR() function returns the year part for a specified date. Syntax

Subtracting years from a date using PHP., How to get current year in PHP ? 25-09-2019. The Date is an inbuilt function used to format the timestamp. The computer stores date and time in UNIX� Possible Duplicate: How do I use PHP to get the current year? 2009 this year, 2010 next year.

Comments
  • Yes, I have maximum input 2069
  • I already tried this but it is giving me 1970 that it is why php can't read the year above the 2038. right?
  • @Shakti: Because as i have said in my answer, PHP can handle year up to 2038 and you have specified 2068. You can test it here: codepad.org/iwqtML6s
  • ya, I agree with you but I have maximum input 2069. so I think I have to use substr or similar function which can split it but the problem is input date fromat can be changed now it is yyyy-mm-dd may be later changed to mm-dd-yyyy
  • @Shakti: As i have shown in my answre later, you can get the date with explode function too: $parts = explode('-', '2068-06-15'); echo $parts[0];. You can test it here: codepad.org/HbS63y2n
  • I do not understand why you're using unix timestamps when the OP has given an example outside of it's range.
  • Note that split is deprecated.