search
Homephp教程php手册php中查询时间

php中查询时间

Jun 06, 2016 pm 07:46 PM
phpaccomplishtimeInquire

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

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

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 "
";

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment