无法使用PHP将日期与时间匹配
我需要将(=或>)日期与今天的日期匹配,并且使用PHP将当前小时与用户定义的值匹配。我在下面解释我的代码无法使用PHP将日期与时间匹配,php,date,Php,Date,我需要将(=或>)日期与今天的日期匹配,并且使用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
<?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) 定义的小时等于用户定义的小时。您可以使用DateTime::createFromFormat比较日期和时间 尝试:
date\u default\u timezone\u set('UTC');//设置与输入日期相同的时区以进行比较很重要
$currentDateTime=new DateTime();//当前日期时间对象
$cdate='2018-08-29 07:50:59';//输入日期
$cdateFormat='Y-m-dh:i:s';//输入日期格式
$cdateDateTime=DateTime::createFromFormat($cdateFormat,$cdate);
$dateFormat='Y-m-d'//仅日期格式
$cdateOnlyDate=$cdateDateTime->format($dateFormat);
$currentDateTimeOnlyDate=$currentDateTime->format($dateFormat);
如果($currentDateTimeOnlyDate<$cdateOnlyDate){
呼应“较短日期”;
}elseif($currentDateTimeOnlyDate>$cdateOnlyDate){
回应“更大的日期”;
}否则{
呼应“同一日期”;
$timeFormat='H:i:s';//仅时间格式
$currentDateTimeOnlyTime=$currentDateTime->format($timeFormat);
$cdateOnlyTime=$cdateDateTime->format($timeFormat);
echo“
检查下面的var_转储,确保w.r.t时区的时间正确
”;
变量转储($currentDateTimeOnlyTime,$cdateOnlyTime);
回声“
”;
如果($currentDateTimeOnlyTime<$cdateOnlyTime){
呼应“更少的时间”;
}elseif($currentDateTimeOnlyTime>$cdateOnlyTime){
呼应“更大的时间”;
}否则{
呼应“同一时间”;
}
}
输出:像这样的东西可以工作
<?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 */
}
?>
有一个
$today1=strtotime($today1);
$today1不是定义变量,因此更改如下
$today1=strtotime($today);
然后它就可以工作了。为什么不使用更简单的方法呢
附加:对于strotime
,DateTime
和date\u创建
我很快为您创建了一些示例和一些注释
<?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 />';
}
}
- 第一个问题已经被多次提到:$Today 1
是未定义的
- 第二个问题是在时间格式中使用h:它永远不会以这种方式工作
- 此外,还有一种日期格式,如
一个你正在使用的,有序的,字符串比较顺序是
与时间顺序相同。因此,没有必要转换为
时间对象
当然,这个版本不仅比较小时,还比较分钟和秒,如果这是一个问题,只需剪切字符串即可
<?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';
}
?>
您实际上并不需要一个函数来完成此操作,但它只是为了进行多次测试:
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
same day
same hour
$today=date(“Y-m-dh:i:s”)有什么意义代码>和$today1=标准时间($today1)
何时可以使用time()
?是否确实要$today=date(“Y-m-d h:i:s”)代码>h
是12小时格式,没有am
或pm
这似乎很简单useless@kerbholz:其24小时格式。->strotime
函数中的“带前导零01到12的小时的12小时格式”和“带前导零00到23的小时的24小时格式”$today1
变量未定义…我还将小时与当前小时进行比较。您可以检查$cdate='2018-08-29 00:00'代码>它还显示了更大的一天“
。完整的带时间比较的更正代码正在进行中。检查。。。即将发布…@subhra,现在检查。我只有2018-08-29 00:00:00
格式是用户指定的日期时间。我的要求是计算其等于/小于今天的日期,并且无论这里提到的小时与当前小时是否匹配。如果用户定义的日期和时间是Y-m-d H:i:s
格式,那么您可以在第二个循环中使用它,与新日期时间('now')
相比$cdate='2018-08-29 00:00'$现在=新日期时间(“现在”)$testDate=新的日期时间($cdate);if($testDate>$now){echo'testDate'.$testDate->format('Y-m-dh:i:s').“大于今天:'.$now->format('Y-m-dh:i:s')。
;}否则if($testDate=$now){echo“equal”}否则{echo 0;}
。它将0打印为输出。其中我的上述代码应打印为equal
。我的另一点也是,用户将匹配给定的时间小时是否为当前小时。它不应打印相等。它正在做它应该做的事情。除非您打算与新日期时间(“今天”)进行比较(请注意使用今天而不是现在)。“今天”有00:00:00表示时间,“现在”有当前时间。
<?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';
}
?>
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