duan0427 2018-12-03 06:57
浏览 60
已采纳

当前时间超出用户元数据数据库时间时运行函数

I have a somewhat complicated time equation that had been driving me mental!

What I have so far is:

$current  = time();    // UTC time
$user_in  = '8:00 am'; // local time
$user_out = '4:00 pm'; // local time
$gmt_off  = '11';      // Australia EST (+1 at the moment)

I then have the function that will convert the local time to UTC by subtracting the GMT offset, and output it as g:i a

function utc( $time ) {
    $time = ( empty($time) ? null : strtotime($time) );
    $gmt  = '60 * 60 * 11'; // there is an actual check for it to be 10 or 11 - and in seconds

    $out = ( $time - $gmt );
    $out = date( 'Y-m-d g:i a', $out );

    return $out;
}

What I cannot figure out is how to properly configure the conditions to check if the current time is outside the $user times

$user_in_utc  = utc( $user_in );
$user_out_utc = utc( $user_out );

if( $current < $user_in_utc && $current > $user_out_utc ) {
    // do something
}

However, the problem I'm running into is that say current time is 6:00pm local time.

How do I check it is now currently less than $user_out when it keeps saying the date is today and not tomorrow?

I intend for these functions to run if the statement being true as a cron task

  • 写回答

1条回答 默认 最新

  • drn1008 2018-12-03 07:43
    关注

    Just use date_default_timezone_set and strtotime to get your timestamp values, and they will all be in the same timezone and hence directly comparable. Note that your condition should use || (or) rather than && (and) to check if the time is outside user hours.

    date_default_timezone_set('Australia/Sydney');
    $current  = strtotime('now');
    $user_in  = strtotime('8:00 am');
    $user_out = strtotime('4:00 pm');
    if( $current < $user_in || $current > $user_out ) {
        // do something
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?