需要此功能在周末返回false(Need this function to return false on weekend days)

编程入门 行业动态 更新时间:2024-10-22 18:37:41
需要此功能在周末返回false(Need this function to return false on weekend days)

需要帮助php。

所以我正在为一家商店写这篇文章。 该函数在关闭时返回false(在9:30开始,在22:45关闭)。

当周的当天是周六或周日时,我还需要它返回false。

这是功能:

function can_order_now_timeframe(){ $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest')); $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest')); $morning_u = $morning->format('U'); $night_u = $night->format('U'); $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest')); $timestampnow = $datenow->format('U'); if(($morning_u<$timestampnow) && ($timestampnow < $night_u)){ return true; }else{ return false; } }

谢谢您的帮助。

Need help with php.

So I am writing this for a shop. This function returns false whenever they are closed (opening at 9:30 and closing at 22:45).

I need it to ALSO return false when the current day of the week is Saturday or Sunday.

Here is the function:

function can_order_now_timeframe(){ $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest')); $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest')); $morning_u = $morning->format('U'); $night_u = $night->format('U'); $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest')); $timestampnow = $datenow->format('U'); if(($morning_u<$timestampnow) && ($timestampnow < $night_u)){ return true; }else{ return false; } }

Thanks for the help.

最满意答案

使用日期对象(信用到Icecub)

function isWeekend($datenow){ $day = $datenow->format('%w'); if ($day=="0"||$day=="6"){ return true; }else{ return false; } }

将date和strtotime与时间戳结合使用($ timestampnow)

如果你有PHP> = 5.1:

function isWeekend($date) { return (date('N', strtotime($date)) >= 6); }

除此以外:

function isWeekend($date) { $weekDay = date('w', strtotime($date)); return ($weekDay == 0 || $weekDay == 6); }

一个完整的代码看起来像。 (您可以在函数内使用函数)

<?php function isWeekend($datenow){ $day = $datenow->format('%w'); if ($day=="0"||$day=="6"){ return true; }else{ return false; } } function can_order_now_timeframe(){ $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest')); $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest')); $morning_u = $morning->format('U'); $night_u = $night->format('U'); $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest')); $timestampnow = $datenow->format('U'); if(($morning_u<$timestampnow) && ($timestampnow < $night_u) && !isWeekend($datenow)){ return true; }else{ return false; } } ?>

或者如果你不认为你可能需要再次进行周末检测,你可以将它全部放在这样的一个函数中:

<?php function can_order_now_timeframe(){ $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest')); $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest')); $morning_u = $morning->format('U'); $night_u = $night->format('U'); $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest')); $timestampnow = $datenow->format('U'); $day = $datenow->format('%w'); if ($day!="0" and $day!="6"){ if(($morning_u<$timestampnow) && ($timestampnow < $night_u)){ return true; }else{ return false; } }else{ return false; } } ?>

Using the date object (credits to Icecub)

function isWeekend($datenow){ $day = $datenow->format('%w'); if ($day=="0"||$day=="6"){ return true; }else{ return false; } }

Using date and strtotime in combination with the timestamp ($timestampnow)

If you have PHP >= 5.1:

function isWeekend($date) { return (date('N', strtotime($date)) >= 6); }

otherwise:

function isWeekend($date) { $weekDay = date('w', strtotime($date)); return ($weekDay == 0 || $weekDay == 6); }

A complete code would look like ths. (You can use a function inside a function)

<?php function isWeekend($datenow){ $day = $datenow->format('%w'); if ($day=="0"||$day=="6"){ return true; }else{ return false; } } function can_order_now_timeframe(){ $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest')); $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest')); $morning_u = $morning->format('U'); $night_u = $night->format('U'); $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest')); $timestampnow = $datenow->format('U'); if(($morning_u<$timestampnow) && ($timestampnow < $night_u) && !isWeekend($datenow)){ return true; }else{ return false; } } ?>

or you can have it all inside one function like this if you dont think you might need the weekend detection again:

<?php function can_order_now_timeframe(){ $morning= new DateTime("09:30", new DateTimeZone('Europe/Budapest')); $night = new DateTime("22:45", new DateTimeZone('Europe/Budapest')); $morning_u = $morning->format('U'); $night_u = $night->format('U'); $datenow = new DateTime("now", new DateTimeZone('Europe/Budapest')); $timestampnow = $datenow->format('U'); $day = $datenow->format('%w'); if ($day!="0" and $day!="6"){ if(($morning_u<$timestampnow) && ($timestampnow < $night_u)){ return true; }else{ return false; } }else{ return false; } } ?>

更多推荐

本文发布于:2023-07-24 10:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:此功能   周末   false   function   weekend

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!