用strtotime()和date()函数算出2019年9月的周日日期
strtotime---用于接收两个参数,第一个参数是格式化的日期数据如:date('Y-m-d'),第二个参数有如'+7 day'
- 函数版
<?php
$firstsunday = strtotime(date('Y-m-01'));
$thisy = intval(date('m'));
$diffdays=;
$count = ;
if (date('w',strtotime(date('Y-m-01')))!=)
{
$diffdays=-date('w',strtotime(date('Y-m-01')));
}
else{
$count++;
}
$startdate =strtotime(date('Y-m-01')."+".$diffdays."day");
while (True)
{
if ($thisy!=intval(date('m',$startdate)))
{
break;
}
echo date('Y-m-d',$startdate)."\n";
$startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-d',$startdate)."+7 day")));
//
}
结果
--
--
--
--
--
- 类版本---date()的参数可以是‘2019-09-20’这样的字符串,如同date('2019-09-20')或者是strtotime返回的整型数如同date('Y-m-d',strtotime('2019-09-20))
class mycalendar
{
function __construct($year,$mon)
{
$this->nianyue=$year.'-'.$mon.'-'.'';
$this->firstday=strtotime(date($this->nianyue));
$this->m = intval(date('m',$this->firstday));
$this->wdaylist=array();
echo "时间戳:".date('Y-m-d',$this->firstday)."\n";
}
function startday()
{
$w = date('w',$this->firstday);
$diffdays=;
if ($w!=)
{
$diffdays= -$w;
}
$this->firstday = strtotime(date('Y-m-d',strtotime($this->nianyue))."+".$diffdays." day");
//array_push($this->wdaylist,$w);
//echo "\n".date('Y-m-d',$this->firstday);
//echo "\n".$this->firstday;
return $this->firstday;
}
function addwday()
{
$curday = $this->startday();
while (true){
if(intval(date('m',$curday)!=$this->m))
{
break;
}
echo date('Y-m-d',$curday)."\n";
array_push($this->wdaylist,date('Y-m-d',$curday));
$curday=strtotime(date('Y-m-d',$curday)."+7 day");
}
return $this->wdaylist;
}
}
$mycal = new mycalendar(,);
var_dump($mycal->addwday());
结果:
时间戳:2019-10-01
2019-10-06
2019-10-13
2019-10-20
2019-10-27
array(4) {
[0]=>
string(10) "2019-10-06"
[1]=>
string(10) "2019-10-13"
[2]=>
string(10) "2019-10-20"
[3]=>
string(10) "2019-10-27"
}
- 计算月末最后一天
echo date('Y-m-d',strtotime(date('2019-10-01')."+1 month -1 day"));
输出结果:
--
- 注意!对于日期计算一定要,正确的使用参数,否则将会出错
$lastday = strtotime(date('Y-m-d',$this->time)."+1 day");#正确
$lastday = strtotime(date($this->time)."+1 day");#错误,这样会返回1970-01-01
用strtotime()和date()函数算出2019年9月的周日日期的更多相关文章
- 一个js函数算出任意位数的水仙花数
一个算出任意位数的水仙花数的函数如下: var arr =[]; /*更改num确定取值范围*/ for(var num = 100; num <= 9999;num++){ /*多位数版本*/ ...
- javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [PHP] 重回基础(date函数和strtotime函数)
date():格式化一个本地时间或者日期,当前时间 2016年5月13日 15:19:49 使用函数date(),输出当前是月份中的第几天,参数:String类型 d 例如:echo date(&qu ...
- PHP date()函数详解
date (PHP 4, PHP 5) date - 格式化一个本地时间/日期 说明¶ string date ( string $format [, int $timestamp ] ) 返回将整数 ...
- PHP 格式化显示时间 date() 函数【转】
date() 函数 功能:用于格式化时间,返回一个字符串. 语法:string date( string format [, int timestamp] ),其中参数 format 表示时间格式化 ...
- oracle date函数
常用的时间格式 在oracle中有 yyyy-mm-dd hh24:mi:ss 而在Java中有些区别 为yyyy-MM-dd HH:mm:ss 这点还是经常容易模糊的.相信很多人都有过统计某些数据 ...
- 【夯实PHP基础】PHP的date函数
本文地址 原文地址 提纲: 1. 引言 2. 代码示例 3. 参考资料 1. 引言 今天看到一段代码 $timeNew = date('n月j日', strtotime($oldTime)); 感觉有 ...
- php date函数 参数详细
time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...
- [R]R下as.Date()函数的坑
问题描述: 在本地使用as.Date()函数从POSIXct类型中提取date时,出现了日期不一致的错误.导致处理数据时,总是出问题. 还好被领导发现数据有误,不然这个bug不知道还要潜伏多久.尽管如 ...
随机推荐
- WUSTOJ 1246: 字符串排序(Java)
1246: 字符串排序 题目 输入n(n<100)个字符串,每个字符串长度不超过1000,将他们按字典顺序输出.更过内容点击标题. 分析 Java中的ArrayList()可以比较方便的 ...
- SAS学习笔记22 t检验、卡方检验
- Solr综合案例深入练习
1. 综合案例 1.1. 需求 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序,并且实现分页功能. 界面如下: 1.2. 分析 开发人员需要 ...
- 音视频入门-03-RGB转成BMP图片
* 音视频入门文章目录 * BMP 文件格式解析 BMP 文件由文件头.位图信息头.颜色信息和图形数据四部分组成. 位图文件头(14个字节) 位图信息头(40个字节) 颜色信息 图形数据 文件头与信息 ...
- C语言数组不知道输入几个整数以及输入一直到为0
输入一直到为0: ){ } 数组不知道输入几个整数: ],num=; for(;;num++){ scanf("%d",&array[num]); if(getchar() ...
- 多节点bigchaindb集群部署
文章比较的长,安装下来大概4个小时左右,我个人使用的服务器,速度会快一点. 安装环境 ostname ip os node-admin 192.168.237.130 ubuntu 18.04.2 d ...
- JS中的原型对象与构造器
在Javascript中:原型对象是属于构造函数的,不属于实例:实例只能共享原型对象中的属性和方法(当然也可以有自己的属性和方法,或者覆盖原型中同名的属性和方法):构造器constructor属于原型 ...
- pytorch中使用多显卡训练以及训练时报错:expect more than 1 value per channel when training, got input size..
pytorch在训练中使用多卡: conf.device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu&quo ...
- JS错误信息类型
1.SyntaxError 语法错误 ①变量名不规范 // 变量名不规范 var 1 = 1; 未被捕获的语法错误,这个错误是js机制自动抛出来的 意外的数字 // 下面两个是同一种情况 var 1a ...
- SourceTree撤销commit
参考链接:http://blog.csdn.net/gang544043963/article/details/71511958 重点是:选中提交之前的版本,再进行撤销回退