如何通过PHP获得半小时的时间 [英] How to get the number of hours untill midnight with PHP

查看:680
本文介绍了如何通过PHP获得半小时的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:记录已输入数据库。

Scenario: An record was entered into the database.

我试图找出以下等式: strong>

I am trying to figure out the following equations:


  1. 如何获取添加记录后的小时数。

  2. 如何自从添加了
    的记录以来,剩下多少小时至午夜。

给定这些时间: strong>

Given these times:


  • 日期/时间:2012-08-22 20:11:20

  • 时间邮票:1345684280

  • 午夜今晚:2012-08-23 00:00:00

  • 午夜时间戳:1345698000

  • Date / Time: 2012-08-22 20:11:20
  • Time Stamp: 1345684280
  • Midnight Tonight: 2012-08-23 00:00:00
  • Midnight Time Stamp: 1345698000

我觉得我在正确的轨道上。只需要一些适当的数学计算呢?我在数学上很可怕任何帮助或指导将不胜感激。我不是在找人来完成这一切。只是寻找关于我做错了什么的建议,或者我怎么能做得更好。也许可以解释实现我的目标所需的数学公式。

I feel like I'm on the right track. Just need some proper math to do the calculations? I am horrible at math. Any help or guidance would be appreciated. I'm not looking for someone to COMPLETE THIS FOR ME. Just looking for advice on what I'm doing wrong, or how I could do it better. Maybe explain the math formulas necessary to achieve my goal.

这是我迄今为止所做的:

class tools{

    public function __construct(){
    }

    public function check_time($time, $request){
        $time = strtotime($time);
        if($request == 'since'){
            $theTime = time() - $time;
            $prefix = 'Since:';
        } elseif($request == 'until'){
            $midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
            $theTime = $midnight - $time;
            $prefix = 'Until:'; 
        }

        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => 'minute',
            1 => 'second'
        );

        foreach($tokens as $unit => $text){
            if($theTime < $unit) continue;
            $duration = floor($theTime / $unit);
            return $prefix.' '.$duration.' '.$text.(($duration>1)?'s':'');
        }
    }
}// EoF tools class

$tools = new tools();

print_r($tools->check_time('2012-08-22 20:11:20', 'since'));
print_r($tools->check_time('2012-08-22 20:11:20', 'until'));


推荐答案

这里的解决方案非常简单。

The solution here is very simple. There is a minor error that's causing all of your issues.

在您的代码中,您可以计算午夜。

In your code you have this to calculate midnight.

$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));

这是不正确的简单的事实,它使用了今天的午夜(这将是今天00:00 ,但是从现在开始的几个小时前,你想要午夜TOMORROW,因为在24小时内被认为是00:00,而且是明天,正确的做法就是这样:

This is incorrect for the simple fact that it's using TODAY's midnight (Which would have been 00:00 today, however many hours ago from now. You want midnight TOMORROW, since it's considered 00:00 on 24 hour time and is tomorrow. The correct way to do it is just like this:

$midnight = strtotime("tomorrow 00:00:00");

请记住,strtotime()基于GMT的所有内容,因此请确保在文件/应用程序中设置默认时区。

Just keep in mind that strtotime() bases everything off of GMT, so make sure you set a default timezone in the file/application.

I希望我的答案很清楚,并解释为什么你发布的代码是错误的,以及如何解决它。

I hope my answer is clear and explains why the code you posted is wrong and how to fix it.

这篇关于如何通过PHP获得半小时的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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