duang8642 2015-01-22 13:54 采纳率: 0%
浏览 50
已采纳

PHP if / else语句带有格式化日期以确定当天的部分

I am trying to make a simple if/else statement in PHP. Someting simple like this:

<?php 
$time = 13;
if ($time < 12) {
  echo "Morning";
} else {
  echo "Afternoon or evening";
}
?>

The problem is the value i receive from the database is an unformatted time format (like: 2014-09-08 06:00:00). I can format this date using:

$time->format('H');

To strip the date from the day, month and year. But you can not use this as a variable. This is what i am trying to do:

<?php $deliverytime = new DateTime('2014-09-08 06:00:00');
$deliverytime->format('H');
if ($deliverytime < 12) {
  echo "Morning";
} else {
  echo "Afternoon or evening";
}
?>

I now this is not working because i am trying to use a formatted date as a variable which does not work. Is there another way to determine the part off the day using a formatted date?

Regards, Matthijs

  • 写回答

4条回答 默认 最新

  • dongtiao2066 2015-01-22 14:00
    关注
    <?php 
         $deliverytime = new DateTime('2014-09-08 06:00:00');
         $hour = $deliverytime->format('H');
         if ($hour < 12) {
           echo "Morning";
         } else {
           echo "Afternoon or evening";
         }
    ?>
    

    $deliverytime->format() is a method that returns the formatted date and you have to store it somewhere to use what that method returns, as the code above shows.

    As it is, you're passing the entire object to the if statement, you need just the result of the method you're using.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳