PHP检查当前时间(PHP check current time)
即时通讯在提交时间检查当前时间方面存在一些小问题
date_default_timezone_set('Asia/Hong_Kong');
$date = date('Y-m-d H:i:s');
我想要这样
if(isset($_POST['submit']) {
if (date (H)< 1pm) {
/* get the time difference or */ echo " you're late $late ";
// do something }
else if (date (H) < 4pm) {
/get the time difference or */ echo " you're late $late ";
// do something }
else if (date (H) < 7pm {
/* get the time difference or */echo " you're late $late ";
// do something }
}
但我不知道如何计算$late和$late是知道提交时间多少小时/分钟
im having a little problem about checking the current time on submitted time
date_default_timezone_set('Asia/Hong_Kong');
$date = date('Y-m-d H:i:s');
i want to to it like this
if(isset($_POST['submit']) {
if (date (H)< 1pm) {
/* get the time difference or */ echo " you're late $late ";
// do something }
else if (date (H) < 4pm) {
/get the time difference or */ echo " you're late $late ";
// do something }
else if (date (H) < 7pm {
/* get the time difference or */echo " you're late $late ";
// do something }
}
but i dont know how to compute the $late and $late is to know how many hours/minute the submitted time
原文:https://stackoverflow.com/questions/42216290
更新时间:2019-12-27 08:51
最满意答案
您可以使用DateTime::createFromFormat()从给定时间创建一个dateobject,并使用new DateTime()进行比较。
$now = new DateTime();
if ($now->format("H:i") > "13:00") {
$deadline = DateTime::createFromFormat("H:i", "13:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
}
如果您在14:04提交表格,则会输出上面的代码片段
你迟到了1小时4分钟
You can use DateTime::createFromFormat() to create a dateobject from the given time, and use new DateTime() to compare against.
$now = new DateTime();
if ($now->format("H:i") > "13:00") {
$deadline = DateTime::createFromFormat("H:i", "13:00");
$diff = $now->diff($deadline);
echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
}
If you submit the form at 14:04, the above snippet would output
You are 1 hours and 4 minutes late
2017-02-14
相关问答
使用BETWEEN SELECT *
FROM lecture
WHERE NOW() BETWEEN starttime AND endtime
use BETWEEN SELECT *
FROM lecture
WHERE NOW() BETWEEN starttime AND endtime
我不明白你为什么要使用全长日期,没有必要。 相反,如何使用unix时间戳? 见http://php.net/manual/en/function.time.php 。 比较可能比较完整的日期字符串很难? 我总是发现使用time()更容易,因为它基本上比较了两个数字。 $current_time = time();
$expiry_possibles_timestamp = strtotime($expiry_possibles[6]);
if ($current_time > $expiry_p
...
您可以使用datetime.weekday()方法和属性: datetime.hour和datetime.minute 。 now = datetime.datetime.now()
if now.weekday() < 5 and now.hour == 18 and 0 <= now.minute <= 15:
do_something()
You may use datetime.weekday() method and attributes: datetime.hour and
...
if(date("G") >= 11) // or >10
{
// current time is greater than 11:00 AM
}
date函数的G参数代表 一小时24小时制,无前导零 手册 if(date("G") >= 11) // or >10
{
// current time is greater than 11:00 AM
}
Where G parameter for date function stands for 24-hour
...
您可以使用DateTime::createFromFormat()从给定时间创建一个dateobject,并使用new DateTime()进行比较。 $now = new DateTime();
if ($now->format("H:i") > "13:00") {
$deadline = DateTime::createFromFormat("H:i", "13:00");
$diff = $now->diff($deadline);
echo "You are ".
...
只需比较时间戳。 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar datetime = Calendar.getInstance();
Calendar c = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.s
...
您可以使用Javascript的Date()对象来实现此目的。 由于澄清问题而进行修订 CSS .active { font-weight: bold; } // Or whatever styling you need
使用Javascript var loop = setInterval(function() {
var d = new Date();
var dh = d.getHours();
var cP = 16;
/* make cP your earliest
...
要将给定时间与当前时间进行比较: if (strtotime($given_time) >= time()+300) echo "You are online";
300是您想要检查的秒数之差。 在这种情况下,5分60秒。 如果要比较两个任意时间,请使用: if (strtotime($timeA) >= strtotime($timeB)+300) echo "You are online";
请注意 :如果时间在不同的日期,例如星期五23:58和星期六00:03,这将失败,因为您只是将时间
...
使用碳 $curdate = Carbon::now();
$discount_db = Discount::whereActive(1)
->where('starting_date', '<=', $curdate)
->where('ending_date', '>=', $curdate)
->first();
if(count($discou
...
这个解决方案 // to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
来自这篇文章: PHP Zend日期格式 这是另一种解决方案: 如何在MM / dd / yyyy中格式化日期HH:mm:ss格式在javascript中? 并在PHP中执行: 如何在PHP 中获取当前日期和时间? This solution: //
...