search
HomeBackend DevelopmentPHP TutorialPHP date format to timestamp_PHP tutorial

PHP provides functions that can easily convert various forms of dates into timestamps. The main functions of this type are:

  • strtotime(): Parses the datetime description of any English text into a timestamp.
  • mktime(): Get timestamp from date.

strtotime()

The strtotime() function is used to convert the date represented by the English text string into a timestamp. It is the inverse function of date() and returns a timestamp successfully, otherwise it returns FALSE. Syntax:

int strtotime ( string time [, int now] )

The parameter time is the parsed string, which is the date represented according to the GNU date input format.

Example:

<?php
echo strtotime("2009-10-21 16:00:10");	//输出 1256112010
echo strtotime("10 September 2008");	//输出 1220976000
echo strtotime("+1 day"), "<br />";	//输出明天此时的时间戳
?>

mktime()

The mktime() function is used to obtain a timestamp from a date and returns the timestamp successfully, otherwise it returns FALSE. Syntax:

int mktime(时, 分, 秒, 月, 日, 年)

Example:

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

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

mktime() is useful for doing date calculations and validation, it will automatically calculate the correct value for out-of-range input. For example, the following example outputs 2008-01-01:

<?php
echo date("Y-m-d", mktime(0, 0, 0, 12, 32, 2007));
echo date("Y-m-d", mktime(0, 0, 0, 13, 1, 2007));
?>

The last day of next month. The last day of any given month can be represented as day "0" of the next month, rather than -1, as in the following example:

<?php
$lastday = mktime(0, 0, 0, 3, 0, 2008);
echo strftime("2008年最后一天是:%d", $lastday);
// 2008年最后一天是:29
?>

Custom function

The following function has similar functions to strtotime.

<?php
$date_str = "2011-09-11 17:00:00";
echo $time_str = str_format_time($date_str);

function str_format_time($timestamp = '')
{   
	if (preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/i", $timestamp)) 
	{
		list($date,$time)=explode(" ",$timestamp);
		list($year,$month,$day)=explode("-",$date);
    	list($hour,$minute,$seconds )=explode(":",$time);
 		$timestamp=gmmktime($hour,$minute,$seconds,$month,$day,$year);
	}
	else
	{
		$timestamp=time();
	}
	return $timestamp;
}

echo '<br />';
echo date("Y-m-d H:i:s", $time_str);

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752397.htmlTechArticlePHP provides functions that can easily convert various forms of dates into timestamps. These functions are mainly: strtotime(): Parses any English text datetime description into a timestamp. ...
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
如何根据当前时间戳创建文件/文件夹并为其命名如何根据当前时间戳创建文件/文件夹并为其命名Apr 27, 2023 pm 11:07 PM

如果您正在寻找根据系统时间戳自动创建文件和文件夹并为其命名的方法,那么您来对地方了。有一种超级简单的方法可以用来完成这项任务。然后,创建的文件夹或文件可用于各种目的,例如存储文件备份、根据日期对文件进行排序等。在本文中,我们将通过一些非常简单的步骤解释如何在Windows11/10中自动创建文件和文件夹,并根据系统的时间戳对其进行命名。使用的方法是批处理脚本,非常简单。希望你喜欢阅读这篇文章。第1节:如何根据系统当前时间戳自动创建文件夹并命名第1步:首先,导航到要在其中创建文件夹的父文件夹,

Golang时间处理:如何在Golang中将时间戳转换为字符串Golang时间处理:如何在Golang中将时间戳转换为字符串Feb 24, 2024 pm 10:42 PM

Golang时间转换:如何将时间戳转换为字符串在Golang中,时间操作是非常常见的操作之一。有时候我们需要将时间戳转换为字符串,以便于展示或者存储。本文将介绍如何使用Golang将时间戳转换为字符串,并提供具体的代码示例。1.时间戳和字符串的转换在Golang中,时间戳通常是以整型数字的形式表示的,表示的是从1970年1月1日至当前时间的秒数。而字符串则

时间戳获取的最佳实践:Golang编程中的强大工具时间戳获取的最佳实践:Golang编程中的强大工具Dec 29, 2023 am 08:28 AM

Golang编程利器:时间戳获取的最佳实践引言:在软件开发中,时间戳是一个非常常见的概念。它是一个标识特定事件发生的数字值,通常表示自从某个参考时间点开始的毫秒数或纳秒数。在Golang中,处理时间戳的操作非常简单且高效。本文将介绍Golang中获取时间戳的最佳实践,并提供具体的代码示例。正文:获取当前时间戳在Golang中,获取当前时间戳非常简单。我们可以

如何从Excel中的日期中删除时间如何从Excel中的日期中删除时间May 17, 2023 am 11:22 AM

使用数字格式更改Excel中的日期格式在Excel中从日期中删除时间的最简单方法是更改​​数字格式。这不会从时间戳中删除时间——它只是阻止它在您的单元格中显示。如果您在计算中使用这些单元格,时间和日期仍然包括在内。要使用数字格式更改Excel中的日期格式:打开您的Excel电子表格。选择包含您的时间戳的单元格。在主菜单中,选择数字格式框末尾的向下箭头。选择一种日期格式。更改格式后,时间将停止出现在您的单元格中。如果单击其中一个单元格,则时间格式在编辑栏中仍然可见。使用单元格格式更

Java文档解读:System类的currentTimeMillis()方法用法解析Java文档解读:System类的currentTimeMillis()方法用法解析Nov 03, 2023 am 09:30 AM

Java文档解读:System类的currentTimeMillis()方法用法解析,需要具体代码示例在Java编程中,System类是一个非常重要的类,其封装了与系统相关的一些属性和操作。其中,currentTimeMillis方法是System类中非常常用的一个方法,本文将对该方法做详细解读并提供代码示例。一.currentTimeMillis方法概述

PHP中的时间戳处理:如何使用strftime函数格式化时间戳为本地化的日期时间PHP中的时间戳处理:如何使用strftime函数格式化时间戳为本地化的日期时间Jul 30, 2023 am 09:15 AM

PHP中的时间戳处理:如何使用strftime函数格式化时间戳为本地化的日期时间在开发PHP应用程序时,我们经常需要处理日期和时间。PHP提供了强大的日期和时间处理函数,其中strftime函数允许我们将时间戳格式化为本地化的日期时间。strftime函数具有以下语法:strftime(string$format[,int$timestamp=ti

js和php怎么将时间戳转换为日期js和php怎么将时间戳转换为日期Mar 21, 2023 pm 05:16 PM

JavaScript和PHP都支持时间戳的处理。JavaScript使用了Date对象来处理日期和时间,而PHP中使用date()函数来格式化时间戳。本文将向大家介绍如何在JavaScript和PHP中进行时间戳与日期之间的转换。

php中的时间戳怎么转换成时间php中的时间戳怎么转换成时间Jan 07, 2023 pm 03:17 PM

在php中,可以使用date()函数来将时间戳转换为日期时间,该函数可以将时间戳格式化为可读性更好的日期和时间;语法格式“date(format,时间戳)”,参数format用于指定格式化字符,设置要转换成的日期格式,例如“Y-m-d H:i:s”,就是将时间戳转为“年-月-日 时:分:秒”格式。

See all articles

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

Hot Tools

MinGW - Minimalist GNU for Windows

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)