![](/img/trans.png)
[英]How do I get the current year plus the next year with php for my wordpress function?
[英]How do I tell PHP's datetime parser that I want the "next" rather than necessarily "the current year's" date?
我從外面得到了這樣的愚蠢日期時間:
Jan-02 19:00
這意味着“下一個 1 月 2 日,19 點”。
目前,由於當前時間是 2021 年底,這意味着2022-01-02 19:00
。 但是如果我將它發送到 PHP 中的日期時間解析器,它會認為我在談論今年的“Jan-02”,即2021-01-02 19:00
。 那是過去。
我自己永遠不會給出一年以下的日期時間,但我無法控制這一點。 也無法聯系他們並讓他們改變任何東西。
這可能非常簡單,但是當日期格式化程序將日期誤解為指過去時,它總是讓我感到悲痛,因為它總是意味着“下一次這個日期從當前時間開始”。
我不知道您可能需要 go 的確切規則,但是看到這樣的日期,我會使用基本規則,如果提供的日期小於當前日期,則將其設置為下一年。 如果輸入日期不是這樣工作的,請隨意使用您自己的規則。
還要記住時區。 您提供的日期時間示例中沒有時區。 它可能與任何時區或 UTC 的服務器默認值不同 - 因此您最終可能會得到與預期不同的時間/日期。
$inputDT = 'Jan-02 19:00';
// Keep timezones in mind!
// The input date has no timezone defined and may differ
// from server or server may be set to UTC.
$dt = DateTime::createFromFormat('M-d H:i', $inputDT);
// If the input date is less than the current date,
// add 1 to the year.
if($dt < (new DateTime())) {
$dt->modify('+1 year');
// Assuming current date is in December 2021,
// $dt is now January 2, 2022 @ 19:00
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.