如何从日期中获取年和月 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8967970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get year and month from a date - PHP
提问by Sara
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 2012and month as January.
从这一天开始,我需要将 year 设为2012,将月份设为January。
回答by Tim Withers
Use strtotime()
:
使用strtotime()
:
$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);
回答by Mob
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
回答by ariestav
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
回答by Hiba
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");
}
回答by Mr.Unknown
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 日 - 星期四
回答by anubhava
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);
回答by Sameera Thilakasiri
$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.
使用explode() 可以做到。
回答by Uday Sawant
$dateValue = '2012-01-05';
$year = date('Y',strtotime($dateValue));
$month = date('F',strtotime($dateValue));
回答by bestfriendkumar
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
回答by Pradeep Bhaskar
$dateValue = strtotime($q);
$yr = date("Y", $dateValue) ." ";
$mon = date("m", $dateValue)." ";
$date = date("d", $dateValue);