久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP:如何檢查日期是今天、昨天還是明天

PHP: How to check if a date is today, yesterday or tomorrow(PHP:如何檢查日期是今天、昨天還是明天)
本文介紹了PHP:如何檢查日期是今天、昨天還是明天的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想檢查一下日期是今天、明天、昨天還是其他日期.但是我的代碼不起作用.

I would like to check, if a date is today, tomorrow, yesterday or else. But my code doesn't work.

代碼:

$timestamp = "2014.09.02T13:34";
$date = date("d.m.Y H:i");
$match_date = date('d.m.Y H:i', strtotime($timestamp));

if($date == $match_date) { 

    //Today

} elseif(strtotime("-1 day", $date) == $match_date) {

    //Yesterday

} elseif(strtotime("+1 day", $date) == $match_date) {

    //Tomorrow

} else {

    //Sometime

}

代碼總是在 else 情況下.

The Code always goes in the else case.

推薦答案

第一. 你在使用函數 strtotime 時出錯了,見 PHP 文檔

First. You have mistake in using function strtotime see PHP documentation

int strtotime ( string $time [, int $now = time() ] )

您需要修改代碼以將整數時間戳傳遞給此函數.

You need modify your code to pass integer timestamp into this function.

第二.您使用包含時間部分的格式 d.m.Y H:i.如果您只想比較日期,則必須刪除時間部分,例如`$date = date("d.m.Y");``

Second. You use format d.m.Y H:i that includes time part. If you wish to compare only dates, you must remove time part, e.g. `$date = date("d.m.Y");``

第三.我不確定它是否對您的工作方式相同,但我的 PHP 無法理解 $timestamp 中的日期格式并返回 01.01.1970 02:00 進入 $match_date

Third. I am not sure if it works in the same way for you, but my PHP doesn't understand date format from $timestamp and returns 01.01.1970 02:00 into $match_date

$timestamp = "2014.09.02T13:34";
date('d.m.Y H:i', strtotime($timestamp)) === "01.01.1970 02:00";

您需要檢查 strtotime($timestamp) 是否返回正確的日期字符串.如果沒有,您需要指定在 $timestamp 變量中使用的格式.您可以使用以下功能之一來執行此操作 date_parse_from_format 或 DateTime::createFromFormat

You need to check if strtotime($timestamp) returns correct date string. If no, you need to specify format which is used in $timestamp variable. You can do this using one of functions date_parse_from_format or DateTime::createFromFormat

這是一個工作示例:

$timestamp = "2014.09.02T13:34";

$today = new DateTime("today"); // This object represents current date/time with time set to midnight

$match_date = DateTime::createFromFormat( "Y.m.d\TH:i", $timestamp );
$match_date->setTime( 0, 0, 0 ); // set time part to midnight, in order to prevent partial comparison

$diff = $today->diff( $match_date );
$diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval

switch( $diffDays ) {
    case 0:
        echo "//Today";
        break;
    case -1:
        echo "//Yesterday";
        break;
    case +1:
        echo "//Tomorrow";
        break;
    default:
        echo "//Sometime";
}

這篇關于PHP:如何檢查日期是今天、昨天還是明天的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 久久精品久久久久久 | 欧美一区二区三 | 色视频一区二区 | 日韩一区在线播放 | av看片网| 欧美精品在线一区二区三区 | 国产精品一区二区不卡 | 国产人成精品一区二区三 | 成人精品网 | 91综合在线观看 | 三级成人片 | 亚洲国产自产 | 国产在线精品一区二区三区 | 日韩欧美三级在线 | 欧美高清一级片 | 中文在线一区二区 | 天天av综合 | 日韩精品一区二区在线观看 | 精品欧美一区二区三区久久久 | 韩日一区二区三区 | 最新超碰 | 99久久婷婷国产综合精品电影 | 99久久婷婷国产综合精品电影 | 亚洲国产中文字幕 | 亚洲一区精品在线 | 亚洲天堂久久新 | 成人在线一区二区三区 | 精品国产一区二区在线 | 黄色日本片 | 一区二区久久电影 | 中文字幕一区二区三区在线观看 | 丁香久久| 在线观看中文字幕视频 | 国产亚洲成av人在线观看导航 | 亚洲在线电影 | 影音先锋欧美资源 | 欧美一区二区三区的 | 日本91av视频 | 中文字幕在线观看一区 | 国产成人精品午夜视频免费 | 成人精品免费视频 |