php 表单datetime,DateTime::createFromFormat

用户评论:

[#1]

Albie at aveit dot org [2015-11-22 18:58:45]

If you want to safely compare equality of a DateTime object without explicitly providing the time portion make use of the ! format character.

$date1=DateTime::createFromFormat('!Y-m-d','2012-10-17');sleep(2);$date2=DateTime::createFromFormat('!Y-m-d','2012-10-17');?>

If you omit the ! format character without explicitly providing the time portion your timestamp which will include the current system time in the stamp.

$date1=DateTime::createFromFormat('Y-m-d','2012-10-17');sleep(2);$date2=DateTime::createFromFormat('Y-m-d','2012-10-17');var_dump($date1==$date2);//will be falsevar_dump($date1>=$date2);//will be falsevar_dump($date1

[#2]

chernomyrdin at gmail dot com [2015-11-19 11:39:15]

$date=DateTime::createFromFormat('U.u',microtime(TRUE));var_dump($date->format('Y-m-d H:i:s.u'));?>

will print: 2015-11-19 11:37:29.125300 (the current time with microseconds)

[#3]

jplevene [2015-06-12 15:09:43]

To convert an email header date use the following (important, notice the * at the end)

$date = DateTime::createFromFormat('D, d M Y H:i:s O *', $email_date);

Some dates in email headers can be formatted as:

Fri, 12 Jun 2015 13:53:37 +0000 (UTC)

The "(UTC)" at the end of the date causes an error and will return a result of false unless the * is at the end.

[#4]

SeliusX [2015-01-30 14:37:46]

Creating timestamps to the day can result in hidden bugs cause hours are taken from now:

Example:

$newDateTime = DateTime::createFromFormat('Y-m-d', '2014-12-10');

$newDateTime->format('H') != '00';

Better use time too or erase the values later on:

$newDateTime = DateTime::createFromFormat('Y-m-d h:i', '2014-12-10 00:00');

or:

$newDateTime = DateTime::createFromFormat(''Y-m-d', '2014-12-10');

$newDateTime->setTime(0, 0);

[#5]

ELPI [2014-10-31 10:04:17]

It can be confusing creating new DateTime from timestamp when your default timezone (date.timezone) is different from UTC and you are used to date()-function.

date()-function automatically uses your current timezone setting but DateTime::createFromFormat (or DateTime constructor) does not (it ignores tz-parameter).

You can get same results as date() by setting the timezone after object creation.

$ts=1414706400;$date1=date("Y-m-d H:i",$ts);$date2=DateTime::createFromFormat("U",$ts)->setTimeZone(newDateTimeZone(date_default_timezone_get()))->format("Y-m-d H:i");//$date1===$date2?>

[#6]

nicodoggie at gmail dot com [2014-06-25 13:20:07]

I've found that on PHP 5.5.13 (not sure if it happens on other versions) if you enter a month larger than 12 on a format that takes numeric months, the result will be a DateTime object with its month equal to the number modulo 12 instead of returning false.

var_dump(DateTime::createFromFormat('Y-m-d','2013-22-01'));?>

results in:

class DateTime#4 (3) {

public $date =>

string(19) "2014-10-01 13:05:05"

public $timezone_type =>

int(3)

public $timezone =>

string(3) "UTC"

}

[#7]

d dot shankarnarayana at gmail dot com [2014-03-24 18:46:43]

Say if there is a string with  $date = "today is 2014 January 1";   and you need to extract "2014 January" using DateTime::createFromFormat().  As you can see in the string there is something odd like "today is" .Since that string (today is) does not correspond to a date format, we need to escape that.

In this case, each and every character on that string has to be escaped as shown below.

The code.

$paragraph = "today is 2014 January 1";

$date = DateTime::createFromFormat('\t\o\d\a\y \i\s Y F j', $paragraph);

echo $date->format('Y F'); //"prints" 2014 January

- Shankar Damodaran

[#8]

mail at marcel-juenemann dot de [2013-01-17 17:24:23]

Note that the U option does not support negative timestamps (before 1970). You have to use date for that.

[#9]

falundir at gmail dot com [2012-10-17 12:03:16]

Be warned that DateTime object created without explicitely providing the time portion will have the current time set instead of 00:00:00.

$date=DateTime::createFromFormat('Y-m-d','2012-10-17');var_dump($date->format('Y-m-d H:i:s'));//will print 2012-10-17 13:57:34 (the current time)?>

That's also why you can't safely compare equality of such DateTime objects:

$date1=DateTime::createFromFormat('Y-m-d','2012-10-17');sleep(2);$date2=DateTime::createFromFormat('Y-m-d','2012-10-17');var_dump($date1==$date2);//will be falsevar_dump($date1>=$date2);//will be falsevar_dump($date1

[#10]

thomas dot ribiere at allgoob dot com [2012-08-30 15:46:35]

Not a bug, but a strange issue today 2012-08-30 :

$date="2011-02";

echo$date."\n";$d=DateTime::createFromFormat("Y-m",$date);

echo$d->format("Y-m");?>

will display :

2011-02

2011-03

It's because there is no 2011-02-30, so datetime will take march insteed of february ...

To fix it :

$date="2011-02";

echo$date."\n";$d=DateTime::createFromFormat("Y-m-d",$date."-01");

echo$d->format("Y-m");?>

[#11]

kamil dot wegrzynowicz at baobaz dot com [2012-02-10 08:05:50]

It seems that a pipe ('|') option in formating string works only with PHP version 5.3.7 and newer. We had an issue with it on versions 5.3.2, 5.3.3, 5.3.6. Yet it was fine with 5.3.8 and 5.3.10.

By short example:

$timezone= newDateTimeZone('UTC');$dateTime=DateTime::createFromFormat('dmY|','01011972',$timezone);//$dateTime is FALSE in PHP v <5.3.8?>

Instead we used a workaround:

$dateTime=DateTime::createFromFormat('dmY','01011972',$timezone);$dateTime->format('Y-m-d 00:00:00');?>

which works fine.

====

Modified by admin to correct for version (5.3.7 not 5.3.8)

[#12]

Aurelien Marchand [2011-03-29 07:13:55]

Beware specifying a timezone in the format as it will take precedence over the DateTimeZone object.

$timezone="UTC";// or any other valid name for a timezone$d=DateTime::createFromFormat("Y-m-d H:i:s T","2011-11-06 00:00:00 EDT",newDateTimeZone($timezone));

echo$d->format("Y-m-d H:i:s T - U");// returns "2011-11-06 00:00:00 EDT - 1320552000"

// specifying $timezone = "Pacific/Honolulu"; would return the same string?>

This gets hairy when you are playing with transition from summer time to winter time! For instance, in Toronto, the time change happens on 2011-11-06. One second after 01:59:59 (EDT), the time becomes 01:00:00 (EST), or 1320559200 in Unix timestamp.

However, notice the following:

$d=DateTime::createFromFormat("Y-m-d H:i:s","2011-11-06 01:00:00",newDateTimeZone("EST"));

echo$d->format("Y-m-d H:i:s T U");// returns "2011-11-06 01:00:00 EDT 1320555600" instead of "2011-11-06 01:00:00 EST 1320559200"

// so the correct way is to do:$d=DateTime::createFromFormat("Y-m-d H:i:s T","2011-11-06 01:00:00 EST",newDateTimeZone($timezone));// set $timezone to any valid string for DateTimeZone, it doesn't matterecho$d->format("Y-m-d H:i:s T U");// returns "2011-11-06 01:00:00 EST - 1320559200" as wanted?>

[#13]

klugg at tlen dot pl [2011-03-14 09:52:30]

In order to use a DateTimeZone, don't enter one of the DateTimeZone::Europe, DateTimeZone::Asia etc. constants, but create a DateTimeZone object with verbal timezone name passed as a string:

$eventDate=DateTime::createFromFormat('m/d/y h:i','02/26/11 08:00', newDateTimeZone('Europe/Warsaw'));

echodate_format($eventDate,'Y-m-d');//prints "2011-02-26"?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值