Table of Contents
Time stamp to date date()
Date to timestamp strtotime()
Home Backend Development PHP Tutorial PHP timestamps and how to convert between dates

PHP timestamps and how to convert between dates

Jul 17, 2018 am 11:30 AM
Time conversion

The conversion between timestamp and date in PHP is realized through the data() function and strtotime() function. The data function realizes the conversion of timestamp to date, and the strtotime function realizes the conversion of date to timestamp.

Time stamp to date date()

  • time()

<?php
var_dump(time()); //获取当前时间戳 int(1502245603)
Copy after login
  • date(format ,timestamp)

format Required. Specifies the format of the timestamp.
timestamp is optional. Specify timestamp. The default is the current time and date

To convert timestamp to date, you can use date('Y-m-s h:i:s', specific timestamp to achieve).

Y : Year (four digits) uppercase
m: month (two digits, add 0 if the first digit is missing) lowercase
d: day (two digits, add 0 if the first digit is missing) lowercase
H: Hour 24-hour hour format with leading zero
h: hour 12-hour hour format with leading zero
i : minute with leading zero
s: seconds with leading zero (00-59)
a: Lowercase noon and afternoon (am or pm)

The distinction between upper and lower case is very important, such as the following example:

var_dump(date(&#39;Y-m-d H:i:s&#39;, 1502204401)); //H 24小时制 2017-08-08 23:00:01 
var_dump(date(&#39;Y-m-d h:i:s&#39;, 1502204401)); //h 12小时制 2017-08-08 11:00:01
Copy after login

Date to timestamp strtotime()

strotime is a very flexible and smart function that can recognize Chinese, English, and numbers. Let’s experience it

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
echo strtotime("20170808 23:00:01"), "\n";
?>
Copy after login

Related recommendations:

How to get the year, month and day in php Timestamp and date methods

Share the method of outputting time by date() function in PHP

The above is the detailed content of PHP timestamps and how to convert between dates. For more information, please follow other related articles on the PHP Chinese website!

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Golang time format conversion: tips for converting date and time Golang time format conversion: tips for converting date and time Feb 24, 2024 pm 06:21 PM

Golang time conversion: Tips for converting dates and times to each other. As the Go language continues to develop in various fields, the need to process dates and times is becoming more and more common. In the Go language, date and time conversion is a basic operation and a common requirement in development. This article will introduce how to convert date and time in Golang, and provide specific code examples to help developers better master this technique. Representation of date and time In Go language, date and time are represented by time.Time respectively.

Share how to quickly convert American time to Chinese time using PHP Share how to quickly convert American time to Chinese time using PHP Mar 28, 2024 am 08:51 AM

PHP is a server-side scripting language widely used in website development. Its flexibility and powerful functions allow developers to quickly implement various functions. In website development, we often encounter the need to convert the time in different countries or regions, such as converting American time to Chinese time. This article will introduce how to use PHP to quickly convert American time to Chinese time, and provide specific code examples. First, we need to clarify the time zone difference between US time and China time. The United States mainly has multiple time zones, including the Eastern

Detailed explanation of how to use the time conversion function in dedecms Detailed explanation of how to use the time conversion function in dedecms Mar 14, 2024 pm 12:33 PM

The content of the article is as follows: Title: Detailed explanation of how to use the time conversion function in DedeCMS. In DedeCMS, time conversion is a common and commonly used function. It can help website developers flexibly process time data and implement operations such as time formatting, time zone conversion, and conversion between timestamps and dates. In this article, we will introduce in detail how to use the time conversion function in DedeCMS and provide specific code examples. 1. For time formatting in DedeCMS, you can use GetDateMK

MySql date and time: how to process and convert MySql date and time: how to process and convert Jun 16, 2023 am 08:48 AM

MySql is a commonly used relational database management system that supports data storage and operations of multiple date and time types. This article will introduce how to process and convert date and time data in MySql. 1. Date and time types MySql supports multiple date and time types, including DATE, TIME, DATETIME, TIMESTAMP, etc. The specific definitions of these types are as follows: DATE: represents date in the format of 'YYYY-MM-DD'. The valid range is '1000-01-

PHP programming skills: Converting American time to Chinese time PHP programming skills: Converting American time to Chinese time Mar 27, 2024 pm 02:27 PM

Title: PHP Programming Tips: Converting American Time to Chinese Time In cross-time zone application development, we often encounter situations where we need to convert times in different time zones. This article will introduce how to use PHP programming to convert American time to Chinese time, and provide specific code examples. First, we need to understand the time difference between US time and China time. The difference between US Eastern Time and China Time is 12 hours, that is, US time is 12 hours behind China time. Therefore, we need to add 12 hours to US time to

What are the methods to convert American time to Chinese time in PHP? What are the methods to convert American time to Chinese time in PHP? Mar 27, 2024 pm 04:18 PM

PHP is a commonly used server-side scripting language used for website development and dynamic web page generation. During the development process, we often encounter situations where we need to convert times in different time zones, such as converting American time to Chinese time. Next, we will introduce some PHP methods to convert American time to Chinese time, and attach specific code examples. Use the DateTime class and DateTimeZone class for conversion: $usDateTime=newDateTime('2023

Time conversion plug-in recommendations and usage tips in dedecms Time conversion plug-in recommendations and usage tips in dedecms Mar 13, 2024 pm 12:06 PM

Time conversion is a common requirement in website development, especially in CMS systems like dedecms. In order to handle the display and conversion of time more conveniently, you can use some time conversion plug-ins. This article will recommend a time conversion plug-in and provide specific usage tips and code examples. Recommended time conversion plug-in: Moment.jsMoment.js is a powerful time processing library that can help developers easily parse, verify, operate and format dates and times in JavaScript.

Golang time conversion: in-depth analysis of time formatting Golang time conversion: in-depth analysis of time formatting Feb 24, 2024 am 10:15 AM

Golang time conversion: Detailed explanation of time formatting operation In the Go language, time processing is a common requirement, and time formatting operation is an important part of it. This article will introduce time conversion and formatting operations in Golang, including time parsing, formatting, time zone conversion, etc., and use specific code examples to help readers better understand. 1. Time analysis In the Go language, the time format has certain specifications, the most common one is the RFC3339 specification. Here is a sample code for time parsing: pa

See all articles