php 如何在php中将日期转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21796958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert date into integer number in php?
提问by ??-Bham
I have an mysql database with a date column and i am using datatype datetime.
我有一个带有日期列的 mysql 数据库,我正在使用数据类型 datetime。
But now i want change my datatype from datetime to long integer
但现在我想将我的数据类型从日期时间更改为长整数
I would like to know how to convert date to any integer value.
我想知道如何将日期转换为任何整数值。
Let say i have a date
假设我有个约会
i.e 2012-03-27 18:47:00
IE 2012-03-27 18:47:00
so I was wondering if it's possible to convert into any integer number like 131221154
所以我想知道是否有可能转换成任何整数 131221154
回答by Vainglory07
Use strtotimefunction of PHP.
使用strtotimePHP的功能。
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 给出的时间戳,或如果现在没有提供当前时间。
echo strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
And to make it back again, just use the datefunction of PHP:
而要让它重新回来,只需使用datePHP的功能:
$long = strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
echo date('Y-m-d H:i:s', $long);
回答by Janak Prajapati
check this
检查这个
<?php
$timestamp = strtotime('1st January 2004'); //1072915200
// this prints the year in a two digit format
// however, as this would start with a "0", it
// only prints "4"
echo idate('y', $timestamp);
?>

