Home > php教程 > php手册 > body text

php中查询时间

WBOY
Release: 2016-06-06 19:46:58
Original
1012 people have browsed it

在做查询过程中,例如要实现查上个月从第一天到最后一天的佣金(提成),那我们在程序实现过程中就要让程序在上个月的范围内查询,第一天是比较好办,但最后一天就不定,要去写段函数进行月份及年份判断来得出上个月共有多少天.那就比麻烦,还有获取当前月份,当前年

在做查询过程中,例如要实现查上个月从第一天到最后一天的佣金(提成),那我们在程序实现过程中就要让程序在上个月的范围内查询,第一天是比较好办,但最后一天就不定,要去写段函数进行月份及年份判断来得出上个月共有多少天.那就比麻烦,还有获取当前月份,当前年份等常规日期获取函数

1.获取本月第一天凌晨0点0分0秒和最后一天23点59分59秒

$BeginDate = date('Y-m-01 00:00:00', strtotime(date("Y-m-d 00:00:00")));
$beginTime = $BeginDate;
$endTime = date('Y-m-d 23:59:59', strtotime("$BeginDate +1 month -1 day"));
var_dump(array('beginTime'=>$beginTime,'endTime'=>$endTime));
输出:array(2) { ["beginTime"]=> string(19) "2014-07-01 00:00:00" ["endTime"]=> string(19) "2014-07-31 23:59:59" }

 

2.获取今天凌晨0点0分0秒和今天23点59分59秒

$beginTime=date('Y-m-d 00:00:00');
$endTime = date('Y-m-d 23:59:59');
var_dump(array('beginTime'=>$beginTime,'endTime'=>$endTime));
输出:array(2) { ["beginTime"]=> string(19) "2014-07-04 00:00:00" ["endTime"]=> string(19) "2014-07-04 23:59:59" }

 

2.获取上个月第一天及最后一天.
echo date('Y-m-01', strtotime('-1 month'));
echo "
";
echo date('Y-m-t', strtotime('-1 month'));
echo "
";
输出:
2014-06-01
2014-06-30


2.获取当月第一天及最后一天.
$BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
echo $BeginDate;
echo "
";
echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
echo "
";
输出:
2014-07-01
2014-07-31

3.获取当天年份、月份、日及天数.
echo " 本月共有:".date("t")."天";
echo " 当前年份".date('Y');
echo " 当前月份".date('m');
echo " 当前几号".date('d');
echo "
";
输出本月共有多少天,当前年份,月份,号份
本月共有:31天
当前年份2014
当前月份07
当前几号04

 

4.获取当前时间和当前月的最后一天

echo date('Y-m-d H:m:s', time());
echo "
";
echo date('Y-m-t', time());
输出:
2014-07-04 15:07:29
2014-07-31

 

4.使用函数及数组来获取当月第一天及最后一天,比较实用,出自网友.
function getthemonth($date)
{
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
return array($firstday,$lastday);
}
$today = date("Y-m-d");
$day=getthemonth($today);
echo "当月的第一天: ".$day[0]." 当月的最后一天: ".$day[1];
echo "
";

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!