如何从日期获得年份和月份 - PHP

时间:2022-05-27 04:34:59

How to get year and month from a given date.

如何从给定日期获得年份和月份。

e.g. $dateValue = '2012-01-05';

例如$ dateValue ='2012-01-05';

From this date I need to get year as 2012 and month as January.

从这一天起,我需要将2012年和月份作为1月份。

10 个解决方案

#1


55  

Use strtotime():

使用strtotime():

$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);

#2


8  

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

使用文档中的date()和strtotime()。

$date = "2012-01-05";

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

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

echo $month

#3


5  

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

#4


4  

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);

#5


4  

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");
    }

#6


4  

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

并将返回一个值:2012年1月5日 - 星期四

#7


2  

$dateValue = '2012-01-05';
$year = date('Y',strtotime($dateValue));
$month = date('F',strtotime($dateValue));

#8


2  

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

Usiong explode()这可以做到。

#9


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

#10


-2  

$dateValue = strtotime($q);

$yr = date("Y", $dateValue) ." "; 
$mon = date("m", $dateValue)." "; 
$date = date("d", $dateValue); 

#1


55  

Use strtotime():

使用strtotime():

$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);

#2


8  

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

使用文档中的date()和strtotime()。

$date = "2012-01-05";

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

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

echo $month

#3


5  

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

#4


4  

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);

#5


4  

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");
    }

#6


4  

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

并将返回一个值:2012年1月5日 - 星期四

#7


2  

$dateValue = '2012-01-05';
$year = date('Y',strtotime($dateValue));
$month = date('F',strtotime($dateValue));

#8


2  

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

Usiong explode()这可以做到。

#9


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

#10


-2  

$dateValue = strtotime($q);

$yr = date("Y", $dateValue) ." "; 
$mon = date("m", $dateValue)." "; 
$date = date("d", $dateValue);