[英]how to extract month and year seperate from a given date in php?
I have a date $da = '18-Nov-2015'
我有一个日期
$da = '18-Nov-2015'
I want month and year separate.I tried this.but it didn't work. 我想将月份和年份分开,但我尝试了一下,但是没有用。
$month = date('F',strtotime($da));
$YEAR = date('Y',strtotime($da));
Give it try with below code: 试试看下面的代码:
$da = '18-Nov-2015';
$date = DateTime::createFromFormat('d-M-Y',$da);
echo $date->format("Y");
echo $date->format("F");
Note:
DateTime
We can create the object using arbitrary parameters like$date = DateTime::createFromFormat('dM-Y', $weird_user_input);
注意:
DateTime
我们可以使用诸如$date = DateTime::createFromFormat('dM-Y', $weird_user_input);
类的任意参数创建对象$date = DateTime::createFromFormat('dM-Y', $weird_user_input);
which can be formatted to unix timestamp or whatever other date format We wish.可以将其格式化为unix时间戳或我们希望的任何其他日期格式。
You can use the date_parse() function. 您可以使用date_parse()函数。 It returns an array that contains the components of the date: day, month, year, hour, minute, and others.
它返回一个数组,其中包含日期的组成部分:日,月,年,小时,分钟和其他。
Usage sample: 用法样本:
$da = '18-Nov-2015';
$dateComps = date_parse($da);
$year = $dateComps['year'];
$month = $dateComps['month'];
$day = $dateComps['day'];
//and so on, ...
Almost you have done.This is how you can do it. 您几乎已经完成了。这就是您可以做到的。
$year = date('Y', strtotime($da));
$month = date('m', strtotime($da));
F
- A full textual representation of a month, such as January or March January through December F
一个月的完整文本表示形式,例如从一月或三月的一月到十二月
m
- Numeric representation of a month, with leading zeros 01 through 12 m
一个月的数字表示形式,前导零从01到12
M
- A short textual representation of a month, three letters Jan through Dec M
一个月的简短文字表示形式,1月至12月为三个字母
n
- Numeric representation of a month, without leading zeros 1 through 12 n
一个月的数字表示,前导零从1到12
Or else simply you can use explode
method 否则就可以使用
explode
方法
$dateArray = explode('-', $da);
$dateArray[0] //date
$dateArray[1] //Month
$dateArray[2] //Year
Date reference : http://php.net/manual/en/function.date.php 日期参考: http : //php.net/manual/en/function.date.php
use 采用
$da=new DateTime($da);
$da->format('m-d');
hello try this hope you will get your answer what you want. 你好,尝试这个希望你会得到你想要的答案。 let me know if you got iy correct.
让我知道你是否正确。
<?php
$dateValue = Date('Y-m-d');
$time=strtotime($dateValue);
$year=date("Y",$time);
$month=date("F",$time);
$date=date("d",$time);
?>
i tried this in http://phpfiddle.org/ 我在http://phpfiddle.org/中尝试过
$da='18-Nov-2015';
$dateValue = strtotime($da);
$year = date('Y',$dateValue);
$monthName = date('F',$dateValue);
$day = date('d',$dateValue);
echo $monthName;
echo $year;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.