![](/img/trans.png)
[英]How can I set Zend DateTime Element to '0000-00-00 00:00:00'?
[英]how to set 00:00:00 for DateTime
我想要得到的是
所以我寫代碼
$today = new \DateTime();
$weekStart = new \DateTime();
$monthStart = new \DateTime();
$yearStart = new \DateTime();
$weekStart->modify('last sunday');
$monthStart->modify('first day of this months');
$yearStart->modify('first day of this year');
print $today->format('Y-m-d h:i:s')."\n";
print $weekStart->format('Y-m-d h:i:s')."\n";
print $monthStart->format('Y-m-d h:i:s')."\n";
print $yearStart->format('Y-m-d h:i:s')."\n";
它給了我結果:
2018-11-13 09:34:02
2018-11-11 12:00:00
2018-11-01 09:34:02
2018-11-01 09:34:02
我有兩個問題。
我怎樣才能使每個DateTime
00:00:00?
如何獲得一年的第一天???
PHP的日期解析有些棘手。 這是您想要執行的代碼片段:
$today = new \DateTime('midnight');
$weekStart = new \DateTime('midnight last sunday');
$monthStart = new \DateTime('midnight first day of this month');
$yearStart = new \DateTime('midnight first day of january this year');
echo $today->format('Y-m-d H:i:s')."\n";
echo $weekStart->format('Y-m-d H:i:s')."\n";
echo $monthStart->format('Y-m-d H:i:s')."\n";
echo $yearStart->format('Y-m-d H:i:s')."\n";
可以在構造時設置DateTime
值; 無需調用DateTime::modify()
。
添加midnight
,將時間設置為00:00:00
。
first day this year
不適用於PHP,您需要改用first day of january this year
。
參考: 相對日期格式的PHP文檔。
$date = new DateTime('2012-07-22');
echo $date->format('Y-m-d 00:00:00');
無需創建datetime()對象,使用上述代碼即可獲取時間為00:00:00的當前日期
你可以試試:
$date->setTime(0, 0, 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.