Owe*_*wen 586
PHP strtotime()给出了
$timestamp = strtotime('22-09-2008');
Run Code Online (Sandbox Code Playgroud)
哪个适用于支持日期和时间格式文档.
Arm*_*her 185
还有一种strptime()预期只有一种格式:
$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
Run Code Online (Sandbox Code Playgroud)
Gor*_*don 123
使用DateTimeAPI:
$dateTime = new DateTime('2008-09-22');
echo $dateTime->format('U');
// or
$date = new DateTime('2008-09-22');
echo $date->getTimestamp();
Run Code Online (Sandbox Code Playgroud)
与过程API相同:
$date = date_create('2008-09-22');
echo date_format($date, 'U');
// or
$date = date_create('2008-09-22');
echo date_timestamp_get($date);
Run Code Online (Sandbox Code Playgroud)
如果由于您使用的格式不受支持而导致上述操作失败,则可以使用
$date = DateTime::createFromFormat('!d-m-Y', '22-09-2008');
echo $dateTime->format('U');
// or
$date = date_parse_from_format('!d-m-Y', '22-09-2008');
echo date_format($date, 'U');
Run Code Online (Sandbox Code Playgroud)
请注意,如果未设置!,则时间部分将设置为当前时间,这与前四个时间不同,后者将在省略时间时使用午夜.
另一种方法是使用IntlDateFormatterAPI:
$formatter = new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'GMT',
IntlDateFormatter::GREGORIAN,
'dd-MM-yyyy'
);
echo $formatter->parse('22-09-2008');
Run Code Online (Sandbox Code Playgroud)
除非您使用本地化日期字符串,否则更容易的选择可能是DateTime.
dar*_*mon 114
小心这样的功能strtotime()试图"猜测"你的意思(它当然没有猜测,规则在这里).
确实22-09-2008会被解析为2008年9月22日,因为这是唯一合理的事情.
怎么08-09-2008解析?可能是2008年8月9日.
怎么样2008-09-50?某些版本的PHP将其解析为2008年10月20日.
因此,如果您确定您的输入是DD-MM-YYYY格式化的,那么最好使用@Armin Ronacher提供的解决方案.
Pro*_*ica 52
如果你有PHP 5.3或以上,
这种方法适用于这两个 Windows和Unix 和是时区意识到,这可能是你想要什么,如果你是严重的使用日期.
如果您不关心时区,或者想要使用服务器使用的时区:
$d = DateTime::createFromFormat('d-m-Y', '22-09-2008');
echo $d->getTimestamp();
Run Code Online (Sandbox Code Playgroud)
1222093324 (这将根据您的服务器时区而有所不同......)
如果要指定在哪个时区,这里是EST.(与纽约相同.)
$d = DateTime::createFromFormat('d-m-Y', '22-09-2008', new DateTimeZone('EST'));
echo $d->getTimestamp();
Run Code Online (Sandbox Code Playgroud)
1222093305
或者如果您想使用UTC.(与"GMT"相同.)
$d = DateTime::createFromFormat('d-m-Y', '22-09-2008', new DateTimeZone('UTC'));
echo $d->getTimestamp();
Run Code Online (Sandbox Code Playgroud)
1222093289
Til*_*ill 43
使用mktime:
list($day, $month, $year) = explode('-', '22-09-2008');
echo mktime(0, 0, 0, $month, $day, $year);
Run Code Online (Sandbox Code Playgroud)
小智 17
使用strtotime()函数可以轻松地将日期转换为时间戳
<?php
// set default timezone
date_default_timezone_set('America/Los_Angeles');
//define date and time
$date = date("d M Y H:i:s");
// output
echo strtotime($date);
?>
Run Code Online (Sandbox Code Playgroud)
更多信息:http://php.net/manual/en/function.strtotime.php
在线转换工具:http://freeonlinetools24.com/
小智 13
这是一个使用split和mtime函数的非常简单有效的解决方案:
$date="30/07/2010 13:24"; //Date example
list($day, $month, $year, $hour, $minute) = split('[/ :]', $date);
//The variables should be arranged according to your date format and so the separators
$timestamp = mktime($hour, $minute, 0, $month, $day, $year);
echo date("r", $timestamp);
Run Code Online (Sandbox Code Playgroud)
它对我来说就像一个魅力.
鉴于该功能strptime()不适用于Windows并且strtotime()可以返回意外结果,我建议使用date_parse_from_format():
$date = date_parse_from_format('d-m-Y', '22-09-2008');
$timestamp = mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
Run Code Online (Sandbox Code Playgroud)
小智 7
使用PHP函数strtotime()
echo strtotime(\'2019/06/06\');\nRun Code Online (Sandbox Code Playgroud)\n\n\n
如果您想确定日期是否被解析为您期望的内容,您可以使用DateTime::createFromFormat():
$d = DateTime::createFromFormat('d-m-Y', '22-09-2008');
if ($d === false) {
die("Woah, that date doesn't look right!");
}
echo $d->format('Y-m-d'), PHP_EOL;
// prints 2008-09-22
Run Code Online (Sandbox Code Playgroud)
在这种情况下显而易见,但例如03-04-2008可能是4月3日或3月4日,具体取决于你来自哪里:)
小智 5
如果你知道格式使用,strptime因为strtotime猜测格式,这可能并不总是正确的.由于strptime未在Windows中实现,因此存在自定义功能
请记住,返回值tm_year是从1900年开始!并且tm_month是0-11
例:
$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900)
Run Code Online (Sandbox Code Playgroud)
小智 5
<?php echo date('M j Y g:i A', strtotime('2013-11-15 13:01:02')); ?>
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.date.php