如何使用strftime以格式1st, 2nd, 25th, 23等显示天数…
?我当前的代码看起来像:
strftime("%B %d, %Y",$expiration_date);
我已经检查了一些解决方案,但不工作
如果你的日期在后台使用时间戳,你可以简单地用PHP的date()
格式化你的日期。它有一个格式字符S
做你所需要的:
date('jS', $timestamp);
将返回类似"1st", "2nd", "3rd"等。
如果你不使用时间戳,olivier的解决方案可能是你最好的选择,或者用strtotime()
把日期变成时间戳。处理后者并不仅仅是为了解决这个问题,但是如果您计划对日期进行算术运算(例如查找两个日期之间的间隔),那么您应该研究一下时间戳。
你可以使用这个函数:
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100), array(11,12,13))) {
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
$expiration_date = time();
$date_str = strftime("%B ", $expiration_date);
$date_str .= addOrdinalNumberSuffix(strftime("%d", $expiration_date));
$date_str .= strftime(", %Y", $expiration_date);
echo $date_str; // July 18th, 2014
?>
这就是说,Lightness Races in Orbit在评论中提到的解决方法对我来说似乎更好。