我需要将日期与今天的日期进行匹配(=或>),而当前小时则使用PHP与用户定义的值进行匹配。我在下面解释我的代码。
<?php
$cdate='2018-08-27 00:00:00';
$today=date("Y-m-d h:i:s");
$cdate1=strtotime($cdate);
$today1=strtotime($today1);
if ($today1 > $cdate1) {
echo 'greater day';
}else{
echo 'lesser day';
}
?>
上面的代码无法正常工作。我的要求是比较
1)今天的日期等于或大于定义的日期
2)定义的小时等于用户定义的小时。
答案 0 :(得分:0)
您可以使用DateTime :: createFromFormat比较日期和时间。
尝试:
date_default_timezone_set('UTC'); // It is important to set same timezone as the input date for comparison
$currentDateTime = new DateTime(); // current DateTime object
$cdate='2018-08-29 07:50:59'; // input date
$cdateFormat = 'Y-m-d H:i:s'; // input date format
$cdateDateTime = DateTime::createFromFormat($cdateFormat, $cdate);
$dateFormat = 'Y-m-d'; //only date format
$cdateOnlyDate = $cdateDateTime->format($dateFormat);
$currentDateTimeOnlyDate = $currentDateTime->format($dateFormat);
if ($currentDateTimeOnlyDate < $cdateOnlyDate) {
echo "lesser date";
} elseif ($currentDateTimeOnlyDate > $cdateOnlyDate) {
echo "greater date";
} else {
echo "same date ";
$timeFormat = 'H:i:s'; //only time format
$currentDateTimeOnlyTime = $currentDateTime->format($timeFormat);
$cdateOnlyTime = $cdateDateTime->format($timeFormat);
echo "<br /> Check the var_dump below to ensure correct time w.r.t timezone<br />";
var_dump($currentDateTimeOnlyTime, $cdateOnlyTime);
echo "<br />";
if ($currentDateTimeOnlyTime < $cdateOnlyTime) {
echo "lesser time";
} elseif ($currentDateTimeOnlyTime > $cdateOnlyTime) {
echo "greater time";
} else {
echo "same time ";
}
}
答案 1 :(得分:0)
类似的事情会起作用
<?php
echo var_dump($date = getdate());
if($date['year'] >= $user_year_value && $date['mon'] >= $user_month_value && $date['mday'] >= $user_day_value){
//** Do something. DOn't forget to set timezone */
}
?>
SQL也可以为您做到这一点 getdate();还会给你小时和分钟。
但是,实际上,如果您打算将其用于约会或事件,则SQL可以完成这项工作。
答案 2 :(得分:0)
有
的问题$today1=strtotime($today1);
$ today1不是定义变量,因此应进行如下更改
$today1=strtotime($today);
然后它起作用。
答案 3 :(得分:0)
为什么不使用DateTime
objects使其更简单?
附加:Relative terms代表strtotime
,DateTime
和date_create
。
我迅速为您创建了一些带有一些注释的示例。
<?php
$today = new DateTime('today'); // comes with today's date and 00:00:00 for time
$now = new DateTime('now'); // comes with today's date and current time
if ($now > $today) {
echo 'Now is later than the day\'s start... <br />';
}
echo '<hr />';
$testDates = [
'2018-08-07',
'2018-08-31',
];
// Compare with "now" -> ignores time
foreach ($testDates as $testDate) {
// Create new DateTime based on given string and sets time to 00:00:00
$testDate = (new DateTime($testDate))->setTime(0, 0, 0);
if ($testDate > $today) {
echo 'Test date "' . $testDate->format('Y-m-d') . '" is greater than today: ' . $today->format('Y-m-d') . '<br />';
}
}
echo '<hr />';
$testDatesWithTime = [
'2018-08-07 12:33:33',
'2018-08-29 08:00:00', // Today - has already been
'2018-08-29 22:00:00', // Today - is yet to come
'2018-08-30 22:00:00',
];
// Compare with "now" -> take time into account
foreach ($testDatesWithTime as $testDate) {
// Create new DateTime based on given string and sets time
$testDate = new DateTime($testDate);
if ($testDate > $now) {
echo 'Test date "' . $testDate->format('Y-m-d H:i:s') . '" is greater than today: ' . $now->format('Y-m-d H:i:s') . '<br />';
}
}
上面的输出
Now is later than the day's start...
Test date "2018-08-31" is greater than today: 2018-08-29
Test date "2018-08-29 22:00:00" is greater than today: 2018-08-29 09:55:06
Test date "2018-08-30 22:00:00" is greater than today: 2018-08-29 09:55:06
答案 4 :(得分:0)
这个版本当然不仅可以比较小时,还可以比较分钟和秒,如果有问题的话,只需剪断字符串即可。
<?php
$cdate='2018-08-29 10:00:00';
$today=date("Y-m-d H:i:s");
if ($today > $cdate) {
echo 'greater day';
} else {
echo 'lesser day';
}
?>
答案 5 :(得分:0)
您实际上并不需要一个函数,但这仅仅是为了进行多次测试:
function compareDate($cdate){
$today=date("Y-m-d h:i:s");
$cdate=array_chunk(date_parse($cdate),3,true);//use date parse to handle any valid date format
$today=array_chunk(date_parse($today),3,true);
$cday=join('-',$cdate[0]);//get only the day without the time
$thisday=join('-',$today[0]);
$cHour=$cdate[1]['hour'];//get only the hour as it is the OP requirement
$thishour=$today[1]['hour'];
if ($thisday > $cday)
{
echo '<br>greater day<br>';
}
elseif($thisday == $cday)//if this day == user defined date compare only Hour then
{
echo '<br>same day<br>';
if ($thishour > $cHour)
{
echo '<br>greater hour<br>';
}
elseif($thishour == $cHour)
{
echo '<br>same hour<br>';
}else
{
echo '<br>lesser hour<br>';
}
}
else
{
echo '<br>lesser day<br>';
}
}
compareDate('2018-08-27 00:00:00');
输出:
greater day
然后
compareDate(date("Y-m-d h:i:s"));
输出:
same day
same hour