周末需要这个功能 return false
Need this function to return false on weekend days
需要 php 方面的帮助。
所以我正在为一家商店写这篇文章。只要它们关闭(在 9:30 打开并在 22:45 关闭),此函数 returns false。
当星期几或星期日时,我还需要 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)){
return true;
}else{
return false;
}
}
感谢您的帮助。
使用日期对象(归功于 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;
}
}
?>
如果你有 PHP >= 5.1:
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) && !(date('N', strtotime($date)) >= 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');
$weekDay = date('w', strtotime($date));
if(($morning_u<$timestampnow) && ($timestampnow < $night_u) && ($weekDay != 0 || $weekDay != 6)){
return true;
}else {
return false;
}
}
需要 php 方面的帮助。
所以我正在为一家商店写这篇文章。只要它们关闭(在 9:30 打开并在 22:45 关闭),此函数 returns false。
当星期几或星期日时,我还需要 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)){
return true;
}else{
return false;
}
}
感谢您的帮助。
使用日期对象(归功于 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;
}
}
?>
如果你有 PHP >= 5.1:
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) && !(date('N', strtotime($date)) >= 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');
$weekDay = date('w', strtotime($date));
if(($morning_u<$timestampnow) && ($timestampnow < $night_u) && ($weekDay != 0 || $weekDay != 6)){
return true;
}else {
return false;
}
}