^\d{4}(年|\-|\)(0[1-9]|1[0-2])(月|\-|\)(0[1-9]|[1-2]\d|3[0-1])日$
这种是还是有缺陷的你可以看到最后一个,还有就是2月31日这样的也会被匹配的。不过应对一般日期还是可以的。
PHP 中的 strtotime() 函数可以实现
strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
strtotime(time,now)
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
成功则返回时间戳,否则返回 FALSE。在 PHP 510 之前本函数在失败时返回 -1。
例子
<php
echo(strtotime("2015-05-22 15:00:00"));
>
/今天/
select from 表名 where to_days(时间字段) = to_days(now());
/昨天/
select from 表名 where to_days(now())-to_days(时间字段) = 1;
/近7天/
select from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段);
/查询距离当前现在6个月的数据/
select from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
/查询当前这周的数据/
select from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());
/查询上周的数据/
select from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;
/查询当前月份的数据/
select from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');
/查询上个月的数据/
select from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');
其它获取类似以上的代码显示
可以在数据库里把时间用 Int 类型存时间戳。
在php里显示的时候,用date()就可以任意显示你想要的格式。
如果在放到mysql里查询,可以先在php中把时间判断好,把你要判断的时候转为时间戳后到数据库里判断。应该要用到strtotime()
¥t = time()+3600*8;&#47;&#47;这里和标准时间相差8小时需要补足¥tget = ¥t-3600*24*5;&#47;&#47;比如5天前的时间echo date(&quot;Y-m-d H:i:s 星期w&quot;,¥tget);&#47;&#47;格式按你需要选取附带:相关时间参数:a - &quot;am&quot; 或是 &quot;pm&quot;A - &quot;AM&quot; 或是 &quot;PM&quot;d - 几日ei二位数字,若不足二位则前面补零; 如: &quot;01&quot; 至 &quot;31&quot;D - 星期几,三个英文字母; 如: &quot;Fri&quot;F - 月份,英文全名; 如: &quot;January&quot;h - 12 小时制的小时; 如: &quot;01&quot; 至 &quot;12&quot;H - 24 小时制的小时; 如: &quot;00&quot; 至 &quot;23&quot;g - 12 小时制的小时,不足二位不补零; 如: &quot;1&quot; 至 12&quot;G - 24 小时制的小时,不足二位不补零; 如: &quot;0&quot; 至 &quot;23&quot;i - 分钟; 如: &quot;00&quot; 至 &quot;59&quot;j - 几日,二位数字,若不足二位不补零; 如: &quot;1&quot; 至 &quot;31&quot;l - 星期几,英文全名; 如: &quot;Friday&quot;m - 月份,二位数字,若不足二位则在前面补零; 如: &quot;01&quot; 至 &quot;12&quot;n - 月份,二位数字,若不足二位则不补零; 如: &quot;1&quot; 至 &quot;12&quot;M - 月份,三个英文字母; 如: &quot;Jan&quot;s - 秒; 如: &quot;00&quot; 至 &quot;59&quot;S - 字尾加英文序数,二个英文字母; 如: &quot;th&quot;,&quot;nd&quot;t - 指定月份的天数; 如: &quot;28&quot; 至 &quot;31&quot;U - 总秒数w - 数字型的星期几,如: &quot;0&quot; (星期日) 至 &quot;6&quot; (星期六)Y - 年,四位数字; 如: &quot;1999&quot;y - 年,二位数字; 如: &quot;99&quot;z - 一年中的第几天; 如: &quot;0&quot; 至 &quot;365&quot;
php获得服务器时间,用到的工具:notepad++,步骤如下:
代码如下:
<php//以中国为时区为示例
date_default_timezone_set("PRC");
echo date("Y年-m月-d日 H:i:s");
>
说明:输出的时间格式为xxxx-xx-xx xx:xx:xx即2017-03-21 20:45:59
2将代码放到一个php文件中,以homephp为示例,放到网站目录下边,运行以后的效果:
注意事项:此代码必须在php环境下运行。
你要实现的是不是当前月份和当前月份往前5个月,每个月的第一天是几号号最后一天是几号?如果是的话,我写了一个 能实现你的需求。你的问题让我好纠结。
$currentTime = time();$cyear = floor(date("Y",$currentTime));
$cMonth = floor(date("m",$currentTime));
for($i=0;$i<6;$i++){
$nMonth = $cMonth-$i;
$cyear = $nMonth == 0 ($cyear-1) : $cyear;
$nMonth = $nMonth <= 0 12+$nMonth : $nMonth;
$date = $cyear"-"$nMonth"-1";
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-t', strtotime($date));
echo $cyear"年"$nMonth"月";
echo "第一天:"$firstday;
echo "最后一天:"$lastday,"<br>";
}
以上就是关于php应用:获取日期正则表达式:\\d{4}[年|\-|\.]\d{\1-\12}[月|\-|\.]\d{\1-\31}日全部的内容,包括:php应用:获取日期正则表达式:\\d{4}[年|\-|\.]\d{\1-\12}[月|\-|\.]\d{\1-\31}日、如何用php把时间戳转化为年月日、如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)