以时间戳格式回显数组/日期时间对象 [英] Echo an array / date time object in timestamp format
问题描述
我有这个 $ date
array:
Array
(
[start] => DateTime对象
(
[date] => 2013-09-19 00:00:00
[timezone_type] => 3
[timezone] =>欧洲/伦敦
)
[end] => DateTime对象
(
[date] => 2013-10-20 23:59:00
[timezone_type] => 3
[时区] =>欧洲/伦敦
)
)$ b我想以时间戳格式回显开始日期值(2013-09-19 00:00:00)$ b
<
我试过 echo $ date ['start'] - > date-> getTimestamp();
但它会返回我这个错误:致命错误:调用一个非对象的成员函数getTimestamp()...
您正在寻找:
echo $ date ['start'] - >格式('Ymd H:我:s');
我相信...检查所有可能的格式在手册页面 _
不要让转储愚弄你, DateTime
对象没有公开 date
属性,,你可以在这里看到。但是,它有一个 getTimestamp
方法,它返回一个int,就像 time()
一样, cf手册。
您可以使用任何预定义的常量(所有字符串,表示标准格式),例如:
echo $ data ['end'] - >格式(DateTime :: W3C); // echoes Ym-dTH:i:s + 01:00)
//或cookie格式的时间:
echo $ data ['end'] - > format(DateTime :: COOKIE); // Wednesday,02-Oct-13 12:42:01 GMT
:我根据您的转储中的 +01:00 和 GMT ,将伦敦显示为您的时区...
所以:
$ now = new DateTime;
$ timestamp = time();
echo $ now-> getTimetamp(),'〜=',$ now; //给或取消,可能减1秒
echo $ now-> format('c') ,'或',$ now->格式('Ymd H:i:s');
阅读手册,玩一会儿,很快就会找到 DateTime
类,所有这些都是相关的类(如 DateInterval
, DateTimeImmutable
等(这里的完整列表))是非常有用的事情确实...
我已经把一些键盘放在一起,这里是代码:
$ date = new DateTime('now',new DateTimeZone('Europe / London'))
$ now = time();
如果(!method_exists($ date,'getTimestamp'))
{//键盘运行< PHP5.3,所以getTimestamp方法没有实现
class MyDate extends DateTime
{//不好的做法,扩展核心对象,但只是一个例子:
const MY_DATE_FORMAT ='Ymd H:i:s';
const MY_DATE_TIMESTAMP ='U';
public function __construct(DateTime $ date)
{
parent :: __ construct($ date-> format(self :: MY_DATE_FORMAT),$ date-> getTimezone());
}
/ **
*添加getTimestamp方法,用于> 5.3
* @return int
** /
public function getTimestamp()
{//立即转到父级,不要使用子格式方法(更快)
return(int)parent :: format(self :: MY_DATE_TIMESTAMP);
}
/ **
*覆盖格式方法,为格式
* @return string设置默认值
** /
public function format($ format = self :: MY_FORMAT)
{//作为示例,具有默认格式
return parent :: format($ format);
}
}
$ date = new MyDate($ date);
}
echo $ date->格式(DateTime :: W3C),PHP_EOL
,$ date->格式(DateTime :: COOKIE),PHP_EOL
,$ date - > getTimestamp(),'〜=',$ now;
I have this $date
array:
Array
(
[start] => DateTime Object
(
[date] => 2013-09-19 00:00:00
[timezone_type] => 3
[timezone] => Europe/London
)
[end] => DateTime Object
(
[date] => 2013-10-20 23:59:00
[timezone_type] => 3
[timezone] => Europe/London
)
)
I want to echo the start date value in timestamp format (2013-09-19 00:00:00)
I tried echo $date['start']->date->getTimestamp();
but it returns me this error : Fatal error: Call to a member function getTimestamp() on a non-object in ...
You're looking for:
echo $date['start']->format('Y-m-d H:i:s');
I believe... Check all possible formats here, on the manual page
Don't let the dump fool you, the DateTime
object hasn't got a public date
property, as you can see here. It does, however, have a getTimestamp
method, which returns an int, just like time()
does, cf the manual.
You can use any of the predefined constants (all strings, representing standard formats), for example:
echo $data['end']->format(DateTime::W3C);//echoes Y-m-dTH:i:s+01:00)
//or, a cookie-formatted time:
echo $data['end']->format(DateTime::COOKIE);//Wednesday, 02-Oct-13 12:42:01 GMT
note: I based the +01:00 and GMT on your dump, showing London as your timezone...
So:
$now = new DateTime;
$timestamp = time();
echo $now->getTimetamp(), ' ~= ', $now;//give or take, it might be 1 second less
echo $now->format('c'), ' or ', $now->format('Y-m-d H:i:s');
Read the manual, play around with it for a while, and you'll soon find the DateTime
class, and all of it's related classes (like DateInterval
, DateTimeImmutable
and such (full list here)) are very handy things indeed...
I've put together a little codepad as an example, here's the code:
$date = new DateTime('now', new DateTimeZone('Europe/London'));
$now = time();
if (!method_exists($date, 'getTimestamp'))
{//codepad runs <PHP5.3, so getTimestamp method isn't implemented
class MyDate extends DateTime
{//bad practice, extending core objects, but just as an example:
const MY_DATE_FORMAT = 'Y-m-d H:i:s';
const MY_DATE_TIMESTAMP = 'U';
public function __construct(DateTime $date)
{
parent::__construct($date->format(self::MY_DATE_FORMAT), $date->getTimezone());
}
/**
* Add getTimestamp method, for >5.3
* @return int
**/
public function getTimestamp()
{//immediatly go to parent, don't use child format method (faster)
return (int) parent::format(self::MY_DATE_TIMESTAMP);
}
/**
* override format method, sets default value for format
* @return string
**/
public function format($format = self::MY_FORMAT)
{//just as an example, have a default format
return parent::format($format);
}
}
$date = new MyDate($date);
}
echo $date->format(DateTime::W3C), PHP_EOL
,$date->format(DateTime::COOKIE), PHP_EOL
,$date->getTimestamp(), ' ~= ', $now;
这篇关于以时间戳格式回显数组/日期时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!