将日期直接存储在PHP变量中

时间:2016-05-20 05:55:05

标签: php date

我想在变量中存储特定日期。如果像<intent-filter> <data android:host="www.example.com" /> <data android:scheme="https" /> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="http" android:host="www.example.com" /> </intent-filter> 一样存储,它就像一个字符串,我无法从中提取部分,例如$x="01/01/2016"年,月,日等等。

4 个答案:

答案 0 :(得分:4)

使用DateTime对象:

$dateTime = new DateTime('2016/01/01');

要仅获取部分日期,您可以使用format方法:

echo $dateTime->format('Y'); // it will display 2016

如果您需要使用您在问题中编写的格式创建它,那么您可以使用工厂方法createFromFormat

$dateTime = DateTime::createFromFormat('d/m/Y', '01/01/2016');
echo $dateTime->format('Y/m/d');

答案 1 :(得分:0)

这对我有用

 $date = '20/May/2015:14:00:01';
    $dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
    $unixTimestamp = mktime(
        $dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
        $dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
        $dateInfo['is_dst']
    );

答案 2 :(得分:0)

答案 3 :(得分:0)

您可以使用$myDate = new DateTime('01/01/2016');来声明日期。要从指定日期获取年,月和日期,请使用echo $myDate->format('d m Y');

根据您的需要更改格式。要了解有关日期格式refer

的更多信息