duan0427
duan0427
2018-12-03 06:57

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

已采纳

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 drn1008 3年前

    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
    }
    
    点赞 评论 复制链接分享

相关推荐