search
HomeBackend DevelopmentPHP ProblemHow to convert time format to timestamp in php?

How to convert time format to timestamp in php?

Nov 06, 2020 pm 03:03 PM
phpTimestampTime format

How to convert PHP time format to timestamp: 1. Use mktime() to convert time to timestamp, syntax "mktime(hour, minute, second, month, day, year)"; 2. Use strtotime() converts a date represented by a string into a timestamp, using the syntax "strtotime (date/time string)".

How to convert time format to timestamp in php?

Recommended: "PHP Video Tutorial"

Method 1: Use the mktime() function

mktime() function returns the UNIX timestamp of a date

Syntax

mktime(hour,minute,second,month,day,year,is_dst);

Parameters:

  • hour : Optional. Specified hours.

  • minute: Optional. prescribed points.

  • second: Optional. Specifies seconds.

  • month: Optional. Specified month.

  • day: Optional. Specify days.

  • year: Optional. Specified year.

  • is_dst: Optional. Set to 1 if the time is during daylight saving time, 0 otherwise, or -1 (default) if unknown. If unknown, PHP will try to find it itself (possibly producing unexpected results). Note: This parameter is deprecated in PHP 5.1.0. Instead, new time zone handling features are used.

Note: Parameters can be omitted from right to left. Any omitted parameters will be set to the current value of the local date and time.

Example

<?php
echo mktime(21, 50, 55, 07, 14, 2010);
//输出“1279115455”
?>

Method 2: Using the strtotime() function

The strtotime() function is used to convert dates represented by strings is the timestamp, returns the timestamp successfully, otherwise returns FALSE.

Syntax:

int strtotime ( string $time [, int $now = time() ] )

This function expects a string containing a US English date format and attempts to parse it into a Unix timestamp (since January 1 1970 00:00:00 GMT seconds), its value is relative to the time given by the now parameter. If this parameter is not provided, the current system time is used.

This function will use the TZ environment variable (if any) to calculate the timestamp. Since PHP 5.1.0 there is an easier way to define the time zone used in all date/time functions. This process is documented on the date_default_timezone_get() function page.

Parameters:

  • time Required. Specifies a date/time string.

  • #now Optional. Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.

Example:

<?php
echo strtotime("2010-10-1 10:00:00")."<br>";
//输出 1285898400
echo strtotime("10 September 2018")."<br>";
//输出 1536508800
echo strtotime("+1 day");
//输出明天此时的时间戳
?>

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to convert time format to timestamp in php?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools