Detailed explanation of PHP timestamp function
This article mainly shares with you the detailed explanation of the PHP timestamp function, mainly in the form of code. I hope it can help you.
echo "The doomsday timestamp is:".strtotime("2012-12-21")
2. Convert the timestamp to system time
date('Y-m-d H:i:s',"1228348800");
(1) Get the zero o'clock timestamp of the current day
$timetoday = strtotime(date("Y-m-d",time()));
(2) Get the zero o'clock timestamp of tomorrow
$tomorrow = $timetoday + 3600*24;
3. The example of the PHP timestamp function to obtain the English text date and time is as follows:
For comparison, use date to convert the current timestamp and the specified timestamp into system time
(1) Print the timestamp at this time tomorrow strtotime(”+1 day”)
//当前时间 echo date("Y-m-d H:i:s",time()); //明天此时时间 echo date("Y-m-d H:i:s",strtotime("+1 day"));
(2) Print the timestamp at this time yesterday strtotime("-1 day")
//当前时间 echo date("Y-m-d H:i:s",time()) ; //指定时间 echo date("Y-m-d H:i:s",strtotime("-1 day"));
(3) Print the timestamp at this time next week strtotime("+1 week")
//当前时间 echo date("Y-m-d H:i:s",time()); //下星期时间 echo date("Y-m-d H:i:s",strtotime("+1 week"));
(4) Print the timestamp of last week at this time strtotime("-1 week")
//当前时间 echo date("Y-m-d H:i:s",time()); //上个星期此时时间 echo date("Y-m-d H:i:s",strtotime("-1 week"));
(5) Print the timestamp of the specified day of the next week strtotime("next Thursday")
//当前时间 echo date("Y-m-d H:i:s",time()); //下星期几时间 echo date("Y-m-d H:i:s",strtotime("next Thursday"));
(6) Print the timestamp of the specified day of the week strtotime("last Thursday")
//当前时间 echo date("Y-m-d H:i:s",time()); //指定时间 echo date("Y-m-d H:i:s",strtotime("last Thursday"));
As can be seen from the above PHP timestamp function example, strtotime can parse the date and time description of any English text For Unix timestamps, we use mktime() or date() to format the date and time to obtain the specified timestamp and achieve the required date and time.
I saw such a function written by others and tested it. Some minor glitches: For New Year's Eve dates, the year is not displayed. Modify as follows
function mdate($time = NULL) { $text = ''; $time = $time === NULL || $time > time() ? time() : intval($time); $t = time() - $time; //时间差 (秒) $y = date('Y', $time)-date('Y', time());//是否跨年 switch($t){ case $t == 0: $text = '刚刚'; break; case $t < 60: $text = $t . '秒前'; // 一分钟内 break; case $t < 60 * 60: $text = floor($t / 60) . '分钟前'; //一小时内 break; case $t < 60 * 60 * 24: $text = floor($t / (60 * 60)) . '小时前'; // 一天内 break; case $t < 60 * 60 * 24 * 3: $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天 break; case $t < 60 * 60 * 24 * 30: $text = date('m月d日 H:i', $time); //一个月内 break; case $t < 60 * 60 * 24 * 365&&$y==0: $text = date('m月d日', $time); //一年内 break; default: $text = date('Y年m月d日', $time); //一年以前 break; } return $text; }
ThinkPHP:
Put the written function in the Common folder. The system will load it automatically.
Put it in this common.php page, common.php has its own format, do not change the name.
Call directly in the template
{$vo.time|mdate}
Example 2, a simpler one
function formatDate($sTime) { //sTime=源时间,cTime=当前时间,dTime=时间差 $cTime = time(); $dTime = $cTime - $sTime; $dDay = intval(date("Ymd",$cTime)) - intval(date("Ymd",$sTime)); $dYear = intval(date("Y",$cTime)) - intval(date("Y",$sTime)); if( $dTime < 60 ){ $dTime = $dTime."秒前"; }elseif( $dTime < 3600 ){ $dTime = intval($dTime/60)."分钟前"; }elseif( $dTime >= 3600 && $dDay == 0 ){ $dTime = "今天".date("H:i",$sTime); }elseif($dYear==0){ $dTime = date("m-d H:i",$sTime); }else{ $dTime = date("Y-m-d H:i",$sTime); } return $dTime; }
Here is a time-friendly method for everyone
/** * 友好时间显示 * @param $time * @return bool|string */ function friend_date($time) { if (!$time) return false; $fdate = ''; $d = time() - intval($time); $ld = $time - mktime(0, 0, 0, 0, 0, date('Y')); //得出年 $md = $time - mktime(0, 0, 0, date('m'), 0, date('Y')); //得出月 $byd = $time - mktime(0, 0, 0, date('m'), date('d') - 2, date('Y')); //前天 $yd = $time - mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //昨天 $dd = $time - mktime(0, 0, 0, date('m'), date('d'), date('Y')); //今天 $td = $time - mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); //明天 $atd = $time - mktime(0, 0, 0, date('m'), date('d') + 2, date('Y')); //后天 if ($d == 0) { $fdate = '刚刚'; } else { switch ($d) { case $d < $atd: $fdate = date('Y年m月d日', $time); break; case $d < $td: $fdate = '后天' . date('H:i', $time); break; case $d < 0: $fdate = '明天' . date('H:i', $time); break; case $d < 60: $fdate = $d . '秒前'; break; case $d < 3600: $fdate = floor($d / 60) . '分钟前'; break; case $d < $dd: $fdate = floor($d / 3600) . '小时前'; break; case $d < $yd: $fdate = '昨天' . date('H:i', $time); break; case $d < $byd: $fdate = '前天' . date('H:i', $time); break; case $d < $md: $fdate = date('m月d日 H:i', $time); break; case $d < $ld: $fdate = date('m月d日', $time); break; default: $fdate = date('Y年m月d日', $time); break; } } return $fdate; }
Related recommendations:
Detailed explanation of PHP's function to obtain the current timestamp
The above is the detailed content of Detailed explanation of PHP timestamp function. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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