PHP关于时间计算的结总
php中计算时间差有时候是件很麻烦的事!
不过我相信任何语言,只要撑握了其中规率就能找到办法,绝对是化腐朽为神奇的。
工作中经常要进行日期计算,下面总结几个工作中总结出的经验。先记下免得日后会忘!
1。第一种 计算时间段差(可以是分,秒,天)
$endtime="2004-09-09 18:10:00";$d1=substr($endtime,17,2); //秒$d2=substr($endtime,14,2); //分$d3=substr($endtime,11,2); // 时$d4=substr($endtime,8,2); //日$d5=substr($endtime,5,2); //月$d6=substr($endtime,0,4); //年
echo $d1.-.$d2.-.$d3.-.$d5.-.$d4.-.$d6."n";
echo date("Y-m-d H:i:s")."n";$now_T=mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y")); echo $now_T."n";$now_S=mktime("$d3","$d2","$d1","$d5","$d4","$d6");echo $now_S."n"; $end_TS=($now_S-$now_T)/60; //计算 剩余分钟echo $end_TS;?>
注 $startdate=mktime("0","0","0","1","1","2000"); //所得到的值为从1970-1-1到参数时间的总秒数 那么 化为分 /60 时 /3600 天/3600/24 即可!
若mktime()中的参数缺省,那表示使用当前日期。
2. 如果有数据库就很容易了!若是MSSQL可以使用触发器!用专门计算日期差的函数datediff()便可! 若是MYSQL那就用两个日期字段的差值计算的计算结果保存在另一个数值型字段中!用时调用便可!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
