PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是:
- strtotime():将任何英文文本的日期时间描述解析为时间戳。
- mktime():从日期取得时间戳。
strtotime()
strtotime() 函数用于将英文文本字符串表示的日期转换为时间戳,为 date() 的反函数,成功返回时间戳,否则返回 FALSE 。语法:
1 |
int strtotime ( string time [, int now] ) |
参数 time 为被解析的字符串,是根据 GNU 日期输入格式表示的日期。
例子:
2 |
echo strtotime ( "2009-10-21 16:00:10" ); //输出 1256112010 |
3 |
echo strtotime ( "10 September 2008" ); //输出 1220976000 |
4 |
echo strtotime ( "+1 day" ), "<br />" ; //输出明天此时的时间戳 |
mktime()
mktime() 函数用于从日期取得时间戳,成功返回时间戳,否则返回 FALSE 。语法:
1 |
int mktime (时, 分, 秒, 月, 日, 年) |
例子:
2 |
echo mktime (21, 50, 55, 07, 14, 2010); //输出“1279115455” |
参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。
mktime() 在做日期计算和验证方面很有用,它会自动计算超出范围的输入的正确值。例如下面例子输出的都是 2008-01-01:
2 |
echo date ( "Y-m-d" , mktime (0, 0, 0, 12, 32, 2007)); |
3 |
echo date ( "Y-m-d" , mktime (0, 0, 0, 13, 1, 2007)); |
下个月的最后一天。任何给定月份的最后一天都可以被表示为下个月的第 "0" 天,而不是 -1 天,如下面的例子:
2 |
$lastday = mktime (0, 0, 0, 3, 0, 2008); |
3 |
echo strftime ( "2008年最后一天是:%d" , $lastday ); |
自定义函数
下面的函数与strtotime功能差不多。
02 |
$date_str = "2011-09-11 17:00:00" ; |
03 |
echo $time_str = str_format_time( $date_str ); |
05 |
function str_format_time( $timestamp = '' ) |
07 |
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 )) |
09 |
list( $date , $time )= explode ( " " , $timestamp ); |
10 |
list( $year , $month , $day )= explode ( "-" , $date ); |
11 |
list( $hour , $minute , $seconds )= explode ( ":" , $time ); |
12 |
$timestamp = gmmktime ( $hour , $minute , $seconds , $month , $day , $year ); |
22 |
echo date ( "Y-m-d H:i:s" , $time_str ); |
- javascript中日期格式与时间戳之间的转化
日期格式与时间戳之间的转化 一:日期格式转化为时间戳 function timeTodate(date) { var new_str = date.replace(/:/g,'-'); new_str ...
- js中日期格式与时间戳格式互换
2014-04-23 18:55:49:123 日期格式 1398250549123 时间戳格式 前台显示日期格式,则 function tsToTime(ts) { var da ...
- shell的date日期循环方法:日期格式转时间戳计算,再将时间戳转回日期格式
1,日期对象转时间戳current_day 2,计算增量的时间戳,即循环每步的增量one_day 3,循环体计算,日期变量加增量后重新赋值自己 4,时间戳转回日期格式后输出 current_day=2 ...
- fastjson对象转为json字符串日期格式变为时间戳问题
今天尝试将map集合转为json对象时遇到一个问题.map中的value为日期格式如"2019-03-01",在使用JSONObject.toJSON(map).toString( ...
- 原生webview 日期格式转时间戳兼容问题
需要根据后端返回的日期格式返回相应时间戳 后端返回的数据格式: let dateStr = 2019-04-19T10:39:10.000+0000; 直接new Date(dateStr ).g ...
- nodejs moment 修改时间格式 日期格式与时间戳格式互相转化
node js moment 修改时间格式 日期格式与int格式互相转化 nvm use 8.3 > moment = require('moment') > days = '2019-0 ...
- js日期格式与时间戳相互转换
本文转自:https://blog.csdn.net/Lc_style/article/details/80626748 1.将日期格式转化为时间戳: var date = new Date('201 ...
- APP 和小程序中通过日期格式获取时间戳的一个bug
介绍一下背景:业务逻辑就不多说了,就说关键出问题的一步,需要将 2019-10-10 这个格式转换为时间戳.在不同平台不同场景下问题还很怪异 app上:ios 安卓线上的都有问题 ios模拟器没问题 ...
- js 时间戳转为日期格式
原文:js 时间戳转为日期格式 js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time) ...
随机推荐
- Customizing Navigation Bar and Status Bar
Like many of you, I have been very busy upgrading my apps to make them fit for iOS 7. The latest ver ...
- mysql-5.7.9-winx64 MySQL服务无法启动,服务没有报告任何错误的解决办法
问题背景 最新解压版本的mysql 解压安装的时候报错 D:\mysql-5.7.9-winx64\bin>net start mysql MySQL 服务正在启动 . MySQL 服务无法启动 ...
- hibernate事务
hibernate事务 9.3 Hibernate的事务管理 事务(Transaction)是工作中的基本逻辑单位,可以用于确保数据库能够被正确修改,避免数据只修改了一部分而导致数据不完整,或者在修改 ...
- u1-nav-html
<header id="masthead" class="masthead" role="banner"> <nav cl ...
- bzoj 1493 暴力
我们可以枚举每个点,然后求出这个点到其余点最小消耗的代价,求出比t小的且距离最大的更新答案. /**************************************************** ...
- sqlserver 还原数据库
1.解决什么问题? a.还原数据库的时候老是提示 不能独占 2.解决方案 ALTER DATABASE [ datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE ...
- Linux强制访问控制机制模块分析之mls_type.h
2.4.2 mls_type.h 2.4.2.1文件描述 对于mls_type.h文件,其完整文件名为security/selinux/ss/mls_types.h,该文件定义了MLS策略使用的类型. ...
- codeforce 626E(二分)
E. Simple Skewness time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 我和NLP的故事(转载)
正值ACL录用结果发布,国内的老师和同学们又是一次大丰收,在这里再次恭喜所有论文被录用的老师和同学们!我人品爆发,也收获了自己硕士阶段的第二篇ACL论文.本来只是想单纯分享下自己中论文的喜悦,但没成想 ...
- ios 关联对象运用 objc_setAssociatedObject
点按钮的时候,给alertView添加一个关联对象(被点击这个按钮), objc_setAssociatedObject(alert, &kRepresentedObject, sender, ...