如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢

如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢,第1张

/今天/

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');

其它获取类似以上的代码显示

/

获取指定日期之间的各个月

/

public function get_months($sdate, $edate) {

$range_arr = array();

do {

$monthinfo = $this->get_monthinfo_by_date($sdate);

$end_day = $monthinfo['month_end_day'];

$start = $this->substr_date($monthinfo['month_start_day']);

$end = $this->substr_date($monthinfo['month_end_day']);

$range = "{$start} ~ {$end}";

$range_arr[] = $range;

$sdate = date('Y-m-d', strtotime($sdate'+1 month'));

}while($end_day < $edate);

return $range_arr;

}

/

截取日期中的月份和日

@param string $date

@return string $date

/

public function substr_date($date) {

if ( ! $date) return FALSE;

return date('m-d', strtotime($date));

}

/

检查日期的有效性 YYYY-mm-dd

@param array $date_arr

@return boolean

/

public function check_date($date_arr) {

$invalid_date_arr = array();

foreach ($date_arr as $row) {

$timestamp = strtotime($row);

$standard = date('Y-m-d', $timestamp);

if ($standard != $row) $invalid_date_arr[] = $row;

}

if ( ! empty($invalid_date_arr)) {

die("invalid date -> "print_r($invalid_date_arr, TRUE));

}

}

PHP获取开始和结束时间

//当前时间

$start

=

strtotime(date('Y-m-d

H:i:s'));

//时长,时间长度(秒为单位,例子中为120秒,2分钟后,实际时间可自行修改或程序计算得出)

//如果是1周后,则为$start

+

(7

24

60

60);

$long

=

$start

+

120

//结束时间

$end

=

date('Y-m-d

H:i:s',

$long);

php可以用函数time()来获取Unix

时间戳,但是只能获取当前的,不能填入参数计算

这个查询的sql语句是这样的select from table(你查询资料的数据表名) where addtime(你资料表中添加时间的字段)>=(1号的时间戳) and addtime<=(15号的时间戳),这样你看下你要求出几个变量了:1号的时间戳和15号的时间戳,这样就可以查询出 1号至15号的资料了

如果不刷新,就用ajax去取数据,回来在显示出来

当然也可以点击按钮到别的页面上去,去取数据。

你数据库的数据,必须有一个字段用来存储日期的,取的时候,判断当前日期与数据库的日期,如果在一个月内的,就取出来。

示例代码如下:

<php

//日期

$date="2016-11-11 11:11:11";

//转换成时间戳

$timestrap=strtotime($date);

//格式化,取出月份

echo date('m',$timestrap);

以上就是关于如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢全部的内容,包括:如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢、PHP如何查询连续月份(跨年)、PHP怎么获得一天,一周,一个月的起始和结束的时间戳求高人指点等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9351811.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存