帮助使用PHP if语句验证日期/时间
我一直在阅读PHP文档,但遇到了麻烦。这里是PHP新手 这个脚本的想法非常简单: 周一、周二和周三上午10点前-当天订购船舶。星期三上午10点以后-星期一订购船只。全天周四、周五、周六和周日-周一订购 我试图检查一周中的哪一天和哪一天的时间,告诉用户他们的订单何时发货。我发现当帮助使用PHP if语句验证日期/时间,php,Php,我一直在阅读PHP文档,但遇到了麻烦。这里是PHP新手 这个脚本的想法非常简单: 周一、周二和周三上午10点前-当天订购船舶。星期三上午10点以后-星期一订购船只。全天周四、周五、周六和周日-周一订购 我试图检查一周中的哪一天和哪一天的时间,告诉用户他们的订单何时发货。我发现当$current_time=date(“H”)在整个上午10:00小时内,我收到一个显示所有三个选项的错误: 您的订单将于今天发货 你的订单明天发货 您的订单将于周一发货 我确信这是因为我写得不好的if语句 然后我继续
$current_time=date(“H”)代码>在整个上午10:00小时内,我收到一个显示所有三个选项的错误:
- 您的订单将于今天发货
- 你的订单明天发货
- 您的订单将于周一发货
我确信这是因为我写得不好的if语句
然后我继续使用date(“H:I”)代码>,但无效。我知道我写这样的东西可能有问题:
else if ($d < 4 && $current_time >= 10:00) {
如果($d<4&&$current\u time>=10:00,则为其他情况){
资料来源:
<?
$current_time = date("H:i");
$d = date("N");
if ($d > 3) {
echo('<p>your order will ship monday</p>');
}
else
if ($d < 4 && $current_time >= 10:00) {
if ($d = 1 && $current_time <= 10:00 || $d = 2 && $current_time <= 10:00) {
echo('<p>your order will ship today.</p>');
}
if ($d = 1 && $current_time >= 10:01 || $d = 2 && $current_time >= 10:01) {
echo('<p>your order will ship tomorrow.</p>');
}
if ($d = 3 && $current_time <= 10:00) {
echo('<p>your order will ship monday.</p>');
}
}
?>
我相信这可以精简很多。非常感谢您的帮助。您应该使用PHP()函数将日期字符串转换为一个整数,您可以将其与使用()检索的当前时间整数值进行比较
使用
类似地,对于其他比较。您没有有效的语法,无法在PHP中比较10:00
一个简单的替代方法是使用:
$current_time = date('Hi')
然后比较如下:
$current_time >= 1000
没有冒号
我还认为,你可能在试图对逻辑过于精明时把自己弄糊涂了。如果规则发生变化,那么代码将很难理解和维护
这里有一个非常简单易读的备选方案:
<?php
class ShipDate {
const TODAY = '<p>your order will ship today</p>';
const TOMORROW = '<p>your order will ship tomorrow</p>';
const MONDAY = '<p>your order will ship monday</p>';
}
$time = date('Hi');
switch (date('l')) {
case 'Monday':
case 'Tuesday':
if ($time < 1000)
echo ShipDate::TODAY;
else
echo ShipDate::TOMORROW;
break;
case 'Wednesday':
if ($time < 1000)
echo ShipDate::TODAY;
else
echo ShipDate::MONDAY;
break;
default:
echo ShipDate::MONDAY;
break;
}
比较10:00是无效的语法
尝试将当前时间更改为
$current_time = date("Hi")
然后删除“:”并进行比较
$current_time >= 1000
如果您是PHP新手,希望轻松进入类等,可以使用一个简单的开关,如:
<?php
$time = date("Hi");
$day = date("N"); //sets a numeric value monday = 1 etc
switch ($day) {
case 1:
if ($time < 1000)
{echo "Your order will ship today";}
else
{echo "Your order will ship tomorrow";}
break;
case 2:
if ($time < 1000)
{echo "Your order will ship today";}
else
{echo "Your order will ship tomorrow";}
break;
case 3:
if ($time < 1000)
{echo "Your order will ship today";}
else
{echo "Your order will ship Monday";}
break;
default:
echo "Your order will ship Monday"; //if none of the above match
}
?>
这很好!我想我对if语句有点太挂了…再次感谢!
$current_time >= 1000
<?php
$time = date("Hi");
$day = date("N"); //sets a numeric value monday = 1 etc
switch ($day) {
case 1:
if ($time < 1000)
{echo "Your order will ship today";}
else
{echo "Your order will ship tomorrow";}
break;
case 2:
if ($time < 1000)
{echo "Your order will ship today";}
else
{echo "Your order will ship tomorrow";}
break;
case 3:
if ($time < 1000)
{echo "Your order will ship today";}
else
{echo "Your order will ship Monday";}
break;
default:
echo "Your order will ship Monday"; //if none of the above match
}
?>