簡體   English   中英

如何在PHP中獲取本地時間今天上午11點和明天上午11點的unix時間戳

[英]How to get unix timestamp for today 11 AM and tomorrow 11 AM in local time in PHP

我已經弄清楚,大概我必須使用date函數來獲取今天和明天,而不是使用strtotime將其轉換為unix時間戳。 我只是不明白如何從date刪除時間部分並將其替換為11AM並將其傳遞給strtotime 我已經添加了date_default_timezone_set設置為本地時區。

strtotime()采用各種瘋狂的格式,您可以這樣做:

$stamp1 = strtotime('today 11 am');
$stamp2 = strtotime('tomorrow 11 am');

如果您更喜歡面向對象的方法,則建議使用PHP的DateTime對象。

$today = new DateTime('now');
$today->setTime(11, 0, 0); //hour, minutes, seconds

$tomorrow = new DateTime('tomorrow');
$tomorrow->setTime(11, 0, 0);

$todayTimestamp    = $today->getTimestamp();
$tomorrowTimestamp = $tomorrow->getTimestamp();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM