PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是:

  • strtotime():将任何英文文本的日期时间描述解析为时间戳。
  • mktime():从日期取得时间戳。

strtotime()

strtotime() 函数用于将英文文本字符串表示的日期转换为时间戳,为 date() 的反函数,成功返回时间戳,否则返回 FALSE 。语法:

int strtotime ( string time [, int now] )

参数 time 为被解析的字符串,是根据 GNU 日期输入格式表示的日期。

例子:

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

mktime()

mktime() 函数用于从日期取得时间戳,成功返回时间戳,否则返回 FALSE 。语法:

int mktime(时, 分, 秒, 月, 日, 年)
<?php
echo mktime(21, 50, 55, 07, 14, 2010); //输出“1279115455”
?>

参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。

mktime() 在做日期计算和验证方面很有用,它会自动计算超出范围的输入的正确值。例如下面例子输出的都是 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));
?>

下个月的最后一天。任何给定月份的最后一天都可以被表示为下个月的第 "0" 天,而不是 -1 天,如下面的例子:

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

自定义函数

下面的函数与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); ?>

php字符串转时间戳的更多相关文章

  1. js获取时间和日期,字符串和时间戳之间的转换

    //获取当前时间: var myDate = new Date();//当前时间 var year = myDate.getFullYear();//当前年份 var month = myDate.g ...

  2. JS 时间字符串与时间戳之间的转换

    1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); ...

  3. MySQL日期、字符串、时间戳互转

    平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法:本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串.date转时间戳.字符串转dat ...

  4. 用shell将时间字符串与时间戳互转

    date的详细用户可以参考下面的 http://www.cnblogs.com/xd502djj/archive/2010/12/29/1919478.html date 的具体用法可以查看另外一篇博 ...

  5. js时间格式化工具,时间戳格式化,字符串转时间戳

    在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽…… 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  6. Mysql 时间、字符串、时间戳互转

    时间转字符串 select date_format(now(),'%Y-%m-%d'); 时间转时间戳 select UNIX_TIMESTAMP(now()); 时间戳转时间 ) :: 时间戳转字符 ...

  7. MySQL 日期、字符串、时间戳互转

    背景 原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去 ...

  8. golang的time包:时间字符串和时间戳的相互转换

    本博客转自: https://blog.csdn.net/mirage003/article/details/86073046 package main import ( "log" ...

  9. MySQL——时间、字符串、时间戳相互转换

    一.时间转字符串 select data_format(now(),'%Y-%m-%d %H:%i:%s'); 二.时间转时间戳 select unix_timestamp(now()); 三.字符串 ...

  10. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象

    1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...

随机推荐

  1. 软件——protel 的pcb电路图制作

    近期一直在学习PCB板的绘制.

  2. (错误记录)git push 报错 403

    在push的时候遇到错误: RPC failed; HTTP curl The requested URL returned error: Forbidden 如果是自己创建的项目的话,可以在网上找到 ...

  3. Injection of autowired dependencies failed; autowire 自动注入失败,测试类已初始化过了Spring容器。

    1 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creat ...

  4. 00090_字节输入流InputStream

    1.字节输入流InputStream (1)通过InputStream可以实现把内存中的数据写出到文件: (2)把内存中的数据写出到文件InputStream此抽象类,是表示字节输入流的所有类的超类. ...

  5. [D3] Add image to the node

    We can create node with 'g' container, then append 'image' to the nodes. // Create container for the ...

  6. angular 响应式自定义表单控件—注册头像实例

    1. 组件继承ControlValueAccessor,ControlValueAccessor接口需要实现三个必选方法 writeValue() 用于向元素中写入值,获取表单的元素的元素值 regi ...

  7. 手动删除RMAN备份的方法

    查询 RMAN> list backup; using target database control file instead of recovery catalog List of Back ...

  8. Day1:字符编码

    一.ASCII码 ASCII(American Standard Code for Information Interchange,美国标准信息交换代码),8位,一个字节,最多只能表示255个符号. ...

  9. Maven学习总结(16)——深入理解maven生命周期和插件

    在项目里用了快一年的maven了,最近突然发现maven项目在eclipse中build时非常慢,因为经常用clean install命令来build项目,也没有管那么多,但最近实在受不了乌龟一样的b ...

  10. ThreadPoolExecutor – Java Thread Pool Example(如何使用Executor框架创建一个线程池)

    Java thread pool manages the pool of worker threads, it contains a queue that keeps tasks waiting to ...