取两个变量作为日期和时间,并合并成一个日期 [英] Take two variables for Date and Time and combine to make one date

查看:102
本文介绍了取两个变量作为日期和时间,并合并成一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想采用两个变量,一个代表日期,另一个代表时间,然后将它们组合起来以形成一个日期.

I would like to take two variables, one representing the date, and another for the time, and then combine them to make one date.

然后,我想使用合并的日期和时间检查当前日期和时间是否与合并的日期和时间相差24小时或更短时间.

Then I would like to use that combined date and time to check to see if the current date and time is 24 hours or less away from the combined date and time.

$game_date = $game['date'];
$game_time = $game['time'];

$combined_date_and_time = date('$game_date $game_time');

$ game_date读取的数据存储为:2013/03/28

$game_date reads is stored as so: 03/28/2013

$ game_time读取结果存储为:07:05 pm

$game_time reads is stored as so: 07:05 pm

在输出中,$ combined_date_and_time读取为:

On output, $combined_date_and_time reads as:

$2pm03America/Los_Angeles_28pm31America/Los_Angeles $2pm03America/Los_Angeles_313803America/Los_Angeles

那当然是不对的.

是否可以将两者合并为一个日期然后进行比较?

Is it possible to combine the two into a single date and then compare?

推荐答案

$game_date = game['date'];
$game_time = game['time'];

$combined_date_and_time = $game_date . ' ' . $game_time;
$past_date = strtotime($combined_date_and_time);

$hours = 24;

// Check if date is more recent than the specified number of hours
if ((time() - $past_date) < (60 * 60 * $hours))
{
    echo 'Yes!';
}


不过要小心!如果您的日期曾经以欧洲格式(dd/mm/yyyy)存储,则需要在解析日期之前更改顺序,否则它将无法正常工作.


But, be careful! If your date is ever stored in the European format (dd/mm/yyyy) you'll need to change the order before parsing the date, otherwise it won't work.

如果日期分隔符为-.,则采用欧洲格式.

If the date separator is - or ., then the European format is assumed.

if (strpos($game_date, '-') !== false)
{
    list($day, $month, $year) = explode('-', $game_date);
    $game_date = $month . '/' . $day . '/' . $year;
}
else if (strpos($game_date, '.') !== false)
{
    list($day, $month, $year) = explode('.', $game_date);
    $game_date = $month . '/' . $day . '/' . $year;
}

上面的代码将介于日期变量的初始化和Combined_date_and_time变量之间.

The above code would go in between the initialization of the date variables and the combined_date_and_time variable.

这篇关于取两个变量作为日期和时间,并合并成一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆