<bdo id='0YQMA'></bdo><ul id='0YQMA'></ul>

    <tfoot id='0YQMA'></tfoot>

    1. <legend id='0YQMA'><style id='0YQMA'><dir id='0YQMA'><q id='0YQMA'></q></dir></style></legend>
    2. <small id='0YQMA'></small><noframes id='0YQMA'>

      <i id='0YQMA'><tr id='0YQMA'><dt id='0YQMA'><q id='0YQMA'><span id='0YQMA'><b id='0YQMA'><form id='0YQMA'><ins id='0YQMA'></ins><ul id='0YQMA'></ul><sub id='0YQMA'></sub></form><legend id='0YQMA'></legend><bdo id='0YQMA'><pre id='0YQMA'><center id='0YQMA'></center></pre></bdo></b><th id='0YQMA'></th></span></q></dt></tr></i><div id='0YQMA'><tfoot id='0YQMA'></tfoot><dl id='0YQMA'><fieldset id='0YQMA'></fieldset></dl></div>

      从日期减去一定数量的小时、天、月或年

      Subtracting a certain number of hours, days, months or years from date(从日期减去一定数量的小时、天、月或年)

        <tfoot id='CRIqb'></tfoot>
        • <bdo id='CRIqb'></bdo><ul id='CRIqb'></ul>
              <legend id='CRIqb'><style id='CRIqb'><dir id='CRIqb'><q id='CRIqb'></q></dir></style></legend>

              1. <small id='CRIqb'></small><noframes id='CRIqb'>

                <i id='CRIqb'><tr id='CRIqb'><dt id='CRIqb'><q id='CRIqb'><span id='CRIqb'><b id='CRIqb'><form id='CRIqb'><ins id='CRIqb'></ins><ul id='CRIqb'></ul><sub id='CRIqb'></sub></form><legend id='CRIqb'></legend><bdo id='CRIqb'><pre id='CRIqb'><center id='CRIqb'></center></pre></bdo></b><th id='CRIqb'></th></span></q></dt></tr></i><div id='CRIqb'><tfoot id='CRIqb'></tfoot><dl id='CRIqb'><fieldset id='CRIqb'></fieldset></dl></div>
                  <tbody id='CRIqb'></tbody>

              2. 本文介绍了从日期减去一定数量的小时、天、月或年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试创建一个简单的函数,它返回一个从现在开始减去一定天数的日期,所以像这样,但我不太了解日期类:

                I'm trying to create a simple function which returns me a date with a certain number of subtracted days from now, so something like this but I dont know the date classes well:

                <?
                function get_offset_hours ($hours) {
                    return date ("Y-m-d H:i:s", strtotime (date ("Y-m-d H:i:s") /*and now?*/));
                }
                
                function get_offset_days ($days) {
                    return date ("Y-m-d H:i:s", strtotime (date ("Y-m-d H:i:s") /*and now?*/));
                }
                
                function get_offset_months ($months) {
                    return date ("Y-m-d H:i:s", strtotime (date ("Y-m-d H:i:s") /*and now?*/));
                }
                
                function get_offset_years ($years) {
                    return date ("Y-m-d H:i:s", strtotime (date ("Y-m-d H:i:s") + $years));
                }
                
                print get_offset_years (-30);
                ?>
                

                是否可以做类似的事情?这种功能可以使用多年,但其他时间类型如何做到这一点?

                Is it possible to do something similar to this? this kind of function works for years, but how to do the same with other time types?

                推荐答案

                几个小时:

                function get_offset_hours($hours)
                {
                    return date('Y-m-d H:i:s', time() + 3600 * $hours);
                }
                

                类似的东西在几个小时和几天内都可以正常工作(几天使用 86400),但是对于几个月和一年来说,它有点棘手......

                Something like that will work well for hours and days (use 86400 for days), but for months and year it's a bit trickier...

                你也可以这样做:

                $date = strtotime(date('Y-m-d H:i:s') . ' +1 day');
                $date = strtotime(date('Y-m-d H:i:s') . ' +1 week');
                $date = strtotime(date('Y-m-d H:i:s') . ' +2 weeks');
                $date = strtotime(date('Y-m-d H:i:s') . ' +1 month');
                $date = strtotime(date('Y-m-d H:i:s') . ' +30 days');
                $date = strtotime(date('Y-m-d H:i:s') . ' +1 year');
                
                echo(date('Y-m-d H:i:s', $date));
                

                这篇关于从日期减去一定数量的小时、天、月或年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Converting string to MySQL timestamp format in php(在php中将字符串转换为MySQL时间戳格式)
                datetime to timestamp(日期时间到时间戳)
                PHP timestamp date to user timezone(PHP时间戳日期到用户时区)
                Converting TIMESTAMP to unix time in PHP?(在 PHP 中将 TIMESTAMP 转换为 unix 时间?)
                Convert ISO 8601 to unixtimestamp(将 ISO 8601 转换为 unixtimestamp)
                Finding days between 2 unix timestamps in php(在php中查找2个unix时间戳之间的天数)
                  <tbody id='qjdi7'></tbody>

                    <bdo id='qjdi7'></bdo><ul id='qjdi7'></ul>
                  • <i id='qjdi7'><tr id='qjdi7'><dt id='qjdi7'><q id='qjdi7'><span id='qjdi7'><b id='qjdi7'><form id='qjdi7'><ins id='qjdi7'></ins><ul id='qjdi7'></ul><sub id='qjdi7'></sub></form><legend id='qjdi7'></legend><bdo id='qjdi7'><pre id='qjdi7'><center id='qjdi7'></center></pre></bdo></b><th id='qjdi7'></th></span></q></dt></tr></i><div id='qjdi7'><tfoot id='qjdi7'></tfoot><dl id='qjdi7'><fieldset id='qjdi7'></fieldset></dl></div>

                        <legend id='qjdi7'><style id='qjdi7'><dir id='qjdi7'><q id='qjdi7'></q></dir></style></legend>
                      • <tfoot id='qjdi7'></tfoot>

                          <small id='qjdi7'></small><noframes id='qjdi7'>