免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3705 | 回复: 2
打印 上一主题 下一主题

请问在PHP中怎么样将一个字符串的日期转成timestamp? [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2016-03-08 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-01-11 21:43 |只看该作者 |倒序浏览
如 $a="1997-3-2" 怎么样转成int型的timestamp?
[2bX] 该用户已被删除
2 [报告]
发表于 2004-01-11 23:51 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2004-01-12 02:36 |只看该作者

请问在PHP中怎么样将一个字符串的日期转成timestamp?

strtotime
(PHP 3>;= 3.0.12, PHP 4 )

strtotime --  将任何英文文本的日期时间描述解析为 UNIX 时间戳
说明
int strtotime ( string time [, int now])


本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 UNIX 时间戳。如果 time 的格式是绝对时间则 now 参数不起作用。如果 time 的格式是相对时间则其所相对的时间由 now 提供,或者如果未提供 now 参数时用当前时间。失败时返回 -1。

因为 strtotime() 的行为是依照 GNU 日期语法的,因此请看看 GNU 手册中的 Date Input Formats。这里记述了 time 参数的合法语法。

例子 1. strtotime() 例子

<?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";
?>;  



例子 2. 检查失败

<?php
$str = 'Not Good';
if (($timestamp = strtotime($str)) === -1) {
    echo "The string ($str) is bogus";
} else {
    echo "$str == ". date('l dS of F Y h:i:s A',$timestamp);
}
?>;  



注: 有效的时间戳典型范围是从格林威治时间 1901 年 12 月 13 日 星期五 20:45:54 到 2038年 1 月 19 日 星期二 03:14:07。(该日期根据 32 位有符号整数的最小值和最大值而来。)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP