第一种方法:PHP strtotime()输出指定时间的时间戳
$timestamp = strtotime(\'22-09-2008\');
第二种方法:
使用DateTimeAPI:
$dateTime = new DateTime(\'2008-09-22\');
echo $dateTime->format(\'U\');
// or
$date = new DateTime(\'2008-09-22\');
echo $date->getTimestamp();
与过程API相同:
$date = date_create(\'2008-09-22\');
echo date_format($date, \'U\');
// or
$date = date_create(\'2008-09-22\');
echo date_timestamp_get($date);
如果由于您使用的是不受支持的格式而导致上述操作失败,则可以使用
$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\');
请注意,如果未设置!,则时间部分将设置为当前时间,这与前四个时间不同,后者将在省略时间时使用午夜。
另一种方法是使用IntlDateFormatterAPI:
$formatter = new IntlDateFormatter(
\'en_US\',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
\'GMT\',
IntlDateFormatter::GREGORIAN,
\'dd-MM-yyyy\'
);
echo $formatter->parse(\'22-09-2008\');
除非您使用本地化的日期字符串,否则更容易的选择可能是DateTime。
第三种方法:
使用mktime:
list($day, $month, $year) = explode(\'-\', \'22-09-2008\');
echo mktime(0, 0, 0, $month, $day, $year); 继续阅读与本文标签相同的文章
下一篇 :
微软开源了 Bing 搜索背后的关键算法
-
IBM打造了一艘AI船 打算明天年穿越大西洋
2026-05-14栏目: 教程
-
德国市场不封杀华为5G:公平竞争
2026-05-14栏目: 教程
-
开封市“离婚率”数据大曝光!开封市哪些职业“出轨率”最高名单排行!
2026-05-14栏目: 教程
-
任正非:华为不是家族企业,外籍人士也可以做华为CEO!
2026-05-14栏目: 教程
-
AI×5G,智能互联迎来发展新动能
2026-05-14栏目: 教程
