1、strtotime基本使用

      date_default_timezone_set('PRC'); //设置中国时区
echo "今天:", date("Y-m-d", time()), "<br>";
echo "昨天:", date("Y-m-d", strtotime("-1 day")), "<br>";
echo "明天:", date("Y-m-d", strtotime("+1 day")), "<br>";
echo "一周后:", date("Y-m-d", strtotime("+1 week")), "<br>";
echo "一周零两天四小时两秒后:", date("Y-m-d G:H:s", strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";
echo "下个星期四:", date("Y-m-d", strtotime("next Thursday")), "<br>";
echo "上个周一(大概率本周的周一):" . date("Y-m-d", strtotime("last Monday")) . "<br>";
echo "上周一:" . date("Y-m-d", strtotime("-1 week last Monday")) . "<br>";
echo "一个月前:" . date("Y-m-d", strtotime("2018-03-31 last month")) . "<br>";//一个月前:2018-03-03
echo "一个月后:" . date("Y-m-d", strtotime("2018-01-31 +1 month")) . "<br>";//一个月前:2018-03-03
echo "十年后:" . date("Y-m-d", strtotime("+10 year")) . "<br>"; echo '<hr>';
//省略一些参数
echo date('Y-m-d H:i:s'). '<br/>';
echo strtotime(date('Y')),'----',date('Y-m-d H:i:s',strtotime(date('Y'))). '<br/>';//?????? 20:18:00 从哪来的
echo strtotime(date('Y-m')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m'))). '<br/>';//天 01,时分秒均为00
echo strtotime(date('Y-m-d')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m-d'))). '<br/>';//时分秒均为00
echo strtotime(date('Y-m-d H')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m-d H'))). '<br/>';//时间格式不识别
echo strtotime(date('Y-m-d H:i')),'----',date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i'))). '<br/>';//秒为00

2、本月最后一天

      $date = '2018-2-17';

      echo '本月最后一天:' . date('Y-m-d', strtotime(date('Y-m-01', strtotime('+1 month'))) - 1) . '<br/>';
echo '某月最后一天:' . date('Y-m-d', strtotime(date('Y-m-01', strtotime($date . ' +1 month'))) - 1) . '<br/>'; //某月共多少天,可用于最后一天
echo date('t', strtotime($date)) . '<br>';

3、往后一个月 +1 month逻辑

strtotime +1 month -1 month逻辑:先将日期转换成合法的年月日,如strtotime("2018-02-29 -1 month") 2018-02-29,转换为2018-03-01。+1 month操作为直接将月份+1,再转换为合法的年月日,如2018-01-31 月份加一个月为2018-02-31,转换为合法的日期就是2018-03-03

      echo "一个月后:" . date("Y-m-d", strtotime("2018-01-01 +1 month")) . "<br>";
echo "一个月后:" . date("Y-m-d", strtotime("2018-01-31 +1 month")) . "<br>";
echo "一个月后:" . date("Y-m-d", strtotime("2018-02-01 +1 month")) . "<br>";
echo "一个月后:" . date("Y-m-d", strtotime("2018-02-02 +1 month")) . "<br>";
echo "一个月后:" . date("Y-m-d", strtotime("2018-02-28 +1 month")) . "<br>";
echo "一个月后:" . date("Y-m-d", strtotime("2018-02-28 -1 month")) . "<br>";
echo "一个月后:" . date("Y-m-d", strtotime("2018-02-29 -1 month")) . "<br>";

4、获取一周中某天的日期(如周三)

      $date = '2018-2-17';
var_dump(getOneDayInWeek(7, $date)); /**
* @desc 获得某一日期所在周(周一到周日)中的某天
* @param int $search 查找星期几,周一 1,周二 2。。。周六 6,周日7
* @param null $date 基准日期
* @return bool|string
*/
getOneDayInWeek($search, $date = NULL) {
$date = !$date ? date('Y-m-d') : date('Y-m-d', strtotime($date));
$search = intval($search);
if ($search < 1 || $search > 7 || !$date) {
return FALSE;
}
$weeks = ['', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']; $w = (int)date('w', strtotime($date));
$w === 0 && $w = 7;
$search > $w && $flag = ' next ';
$search < $w && $flag = ' last '; return isset($flag) ? date("Y-m-d", strtotime($date . $flag . $weeks[$search])) : $date;
}

5、类似合同中下月减一天,试用期三个月后减一天

      $deviationMonth = 1;
$deviationDay = -1;
$date = '2018-10-1';
var_dump(getNextMonth($date, $deviationMonth, $deviationDay)); /**
* @desc 类似合同中下月减一天,试用期三个月后减一天
* 正常 2018-01-17 --- 2018-02-16,2017-10-01---2017-10-31,2017-10-31 --- 2017-11-30
* 2018-01-28 --- 2018-02-27,2018-01-29 --- 2018-02-28,2018-01-30 --- 2018-02-28,2018-01-31 --- 2018-02-28,2018-02-01 --- 2018-02-28,2018-02-02 --- 2018-03-01,2018-02-03 --- 2018-03-02
* @param null $date 起始日期
* @param int $deviationMonth 向后推几个月
* @param int $deviationDay 向后推几天,一般是向前一天(-1)
* @return bool|string
*/
getNextMonth($date = NULL, $deviationMonth = 1, $deviationDay = -1) {
$timestamp = !$date ? time() : strtotime($date);
$deviationMonth = intval($deviationMonth);
$deviationDay = intval($deviationDay);
if (!$deviationMonth || !$deviationDay || !$timestamp) {
return FALSE;
}
$month = date('Y-m', strtotime(date('Y-m', $timestamp) . ' ' . $deviationMonth . ' month'));
$day = date('d', $timestamp) + $deviationDay; $day > date('t', strtotime($month)) && $day = date('t', strtotime($month)); return date('Y-m-d', strtotime($month . '-' . $day));
}

参考

PHP的日期操作 增加减少

php 如何计算指定时间,往后1个月的时间戳

最新文章

  1. 实例1-gettree
  2. linux启动流程
  3. HomeKit 与老旧设备
  4. 【leetcode】Minimum Window Substring (hard) ★
  5. MySql的基本操作以及以后开发经常使用的常用指令
  6. 初识selenium-grid
  7. HTML5之WebSocket
  8. 非阻塞式socket的select()用法
  9. 创建Xml的将但方法和向Xml中添加数据
  10. Asp.net Identity 系列之 怎样修改Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的 Id 字段的数据类型
  11. 一篇文章带你快速入门createjs
  12. Win7 JBOSS的下载安装、环境变量配置以及部署
  13. Contest2163 - 2019-3-28 高一noip基础知识点 测试6 题解版
  14. POJ3204 Ikki&#39;s Story I - Road Reconstruction
  15. C# 发送email邮件!
  16. 搭建 FTP 文件服务
  17. react-navigation,StackNavigator,TabNavigator 导航使用
  18. Java面试题,Java三大特性之一——多态的理解
  19. 《剑指offer》— JavaScript(32)把数组排成最小的数
  20. Linux 黑白界面显示

热门文章

  1. ASPNET CORE初探
  2. Android面试题-OkHttp3源码分析
  3. [Android Pro] 分析 Package manager has died
  4. WCF项目中出现“目标程序集不包含服务类型”的解决办法
  5. java 五子棋之人机对战思路详解
  6. Informatica pmcmd命令
  7. 学习笔记4-Action参数绑定
  8. Thinkphp学习笔记2-
  9. 04-树4. Root of AVL Tree (25)
  10. [Algorithm] Construct a Binary Tree and Binary Search