42

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sara
  • 14,098
  • 13
  • 34
  • 50

10 Answers10

85

Use strtotime():

$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);
Tim Withers
  • 12,072
  • 5
  • 43
  • 67
13

Using date() and strtotime() from the docs.

$date = "2012-01-05";

$year = date('Y', strtotime($date));

$month = date('F', strtotime($date));

echo $month
Sara
  • 14,098
  • 13
  • 34
  • 50
Mob
  • 10,958
  • 6
  • 41
  • 58
6

I'm using these function to get year, month, day from the date

you should put them in a class

    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");
    }
Hiba
  • 101
  • 1
  • 2
6

Probably not the most efficient code, but here it goes:

$dateElements = explode('-', $dateValue);
$year = $dateElements[0];

echo $year;    //2012

switch ($dateElements[1]) {

   case '01'    :  $mo = "January";
                   break;

   case '02'    :  $mo = "February";
                   break;

   case '03'    :  $mo = "March";
                   break;

     .
     .
     .

   case '12'    :  $mo = "December";
                   break;


}

echo $mo;      //January
ariestav
  • 2,799
  • 4
  • 28
  • 56
5

I will share my code:

In your given example date:

$dateValue = '2012-01-05';

It will go like this:

dateName($dateValue);



   function dateName($date) {

        $result = "";

        $convert_date = strtotime($date);
        $month = date('F',$convert_date);
        $year = date('Y',$convert_date);
        $name_day = date('l',$convert_date);
        $day = date('j',$convert_date);


        $result = $month . " " . $day . ", " . $year . " - " . $name_day;

        return $result;
    }

and will return a value: January 5, 2012 - Thursday

Mr.Unknown
  • 171
  • 1
  • 2
  • 11
5

You can use this code:

$dateValue = strtotime('2012-06-05');
$year = date('Y',$dateValue);
$monthName = date('F',$dateValue);
$monthNo = date('m',$dateValue);
printf("m=[%s], m=[%d], y=[%s]\n", $monthName, $monthNo, $year);
anubhava
  • 761,203
  • 64
  • 569
  • 643
3
$dateValue = '2012-01-05';
$yeararray = explode("-", $dateValue);

echo "Year : ". $yeararray[0];
echo "Month : ". date( 'F', mktime(0, 0, 0, $yeararray[1]));

Usiong explode() this can be done.

Sameera Thilakasiri
  • 9,452
  • 10
  • 51
  • 86
2
$dateValue = '2012-01-05';
$year = date('Y',strtotime($dateValue));
$month = date('F',strtotime($dateValue));
Uday Sawant
  • 5,748
  • 3
  • 32
  • 45
0

I personally prefer using this shortcut. The output will still be the same, but you don't need to store the month and year in separate variables

$dateValue = '2012-01-05';
$formattedValue = date("F Y", strtotime($dateValue));
echo $formattedValue; //Output should be January 2012

A little side note on using this trick, you can use comma's to separate the month and year like so:

$formattedValue = date("F, Y", strtotime($dateValue));
echo $formattedValue //Output should be January, 2012
dan.m.kumar
  • 758
  • 2
  • 9
  • 20
  • `SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION yii_attendance.DATEPART does not exist The SQL being executed was: SELECT daytime FROM attendance WHERE DATEPART(month, my_date_field) = 8 ORDER BY daytime DESC` – raxa Sep 22 '15 at 19:19
-1
$dateValue = strtotime($q);

$yr = date("Y", $dateValue) ." "; 
$mon = date("m", $dateValue)." "; 
$date = date("d", $dateValue); 
Machavity
  • 30,841
  • 27
  • 92
  • 100
Pradeep Bhaskar
  • 231
  • 2
  • 2