簡體   English   中英

如何將帶變量的小時數添加到日期時間變量

[英]How to Add Hours with Variable to a date time Variable

我的服務器 API 返回一個時間變量作為 GMT 時間。 我首先發現了與當地時間的差異。 然后我想添加小時,但我不知道如何包含差異的變量:

$df = "G:i:s";  // Use a simple time format to find the difference
$tz = 'Europe/Berlin';
$tz_obj = new DateTimeZone($tz);
$today = new DateTime("now", $tz_obj);
$ts1 = $today->format($df); //Locale Time
$ts2 = gmdate($df); // Timestamp of current UTC time
$ts3 = $ts1-$ts2; // The difference are 2

這將起作用:

$newDate = date('Y-m-d H:i:s', strtotime($menue_datum. ' + 2 hours'));

但是我怎樣才能插入我的變量 $ts3?

$newDate = date('Y-m-d H:i:s', strtotime($menue_datum. ' + {$ts3} hours'));

這是一個簡單的解決方案:

$newDate = date('Y-m-d H:i:s', strtotime($menue_datum. ' + ' . $ts3 . 'hours'));

暫無
暫無

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

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