我有一个小函数,它显示最新的活动,它从数据库中获取unix格式的时间戳,然后用以下代码将其输出:
date("G:i:s j M -Y", $last_access)
现在我想要将日期(j M -Y)替换为"Yesterday"(昨天),如果最新活动在今天之内,则替换为"Today"(今天),如果是昨天的同样情况也适用。
我该如何做到这一点?
我有一个小函数,它显示最新的活动,它从数据库中获取unix格式的时间戳,然后用以下代码将其输出:
date("G:i:s j M -Y", $last_access)
现在我想要将日期(j M -Y)替换为"Yesterday"(昨天),如果最新活动在今天之内,则替换为"Today"(今天),如果是昨天的同样情况也适用。
我该如何做到这一点?
如果$last_access
在昨晚12点和前一天的时间戳之间,那么显示昨天,任何大于昨晚12点的时间戳都将显示今天。我认为这比进行日期算术运算更快。
实际上,我刚测试了一下这段代码,它似乎工作得很好:
<?php
if ($last_access >= strtotime("today"))
echo "Today";
else if ($last_access >= strtotime("yesterday"))
echo "Yesterday";
?>
function get_day_name($timestamp) {
$date = date('d/m/Y', $timestamp);
if($date == date('d/m/Y')) {
$date = 'Today';
}
else if($date == date('d/m/Y',now() - (24 * 60 * 60))) {
$date = 'Yesterday';
}
return $date;
}
print date('G:i:s', $last_access).' '.get_day_name($last_access);
else if
这行似乎缺少一个 )
。 - Maerlynreturn $day_name;
吧? - Paulo Bueno你需要将日对比日,秒的比较是完全错误的:
如果我们现在是早上,那么昨晚就是今天(减去24小时)^^
这里是我在Kinoulink(一家法国初创公司)使用的方法:
public function formatDateAgo($value)
{
$time = strtotime($value);
$d = new \DateTime($value);
$weekDays = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche'];
$months = ['Janvier', 'Février', 'Mars', 'Avril',' Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];
if ($time > strtotime('-2 minutes'))
{
return 'Il y a quelques secondes';
}
elseif ($time > strtotime('-30 minutes'))
{
return 'Il y a ' . floor((strtotime('now') - $time)/60) . ' min';
}
elseif ($time > strtotime('today'))
{
return $d->format('G:i');
}
elseif ($time > strtotime('yesterday'))
{
return 'Hier, ' . $d->format('G:i');
}
elseif ($time > strtotime('this week'))
{
return $weekDays[$d->format('N') - 1] . ', ' . $d->format('G:i');
}
else
{
return $d->format('j') . ' ' . $months[$d->format('n') - 1] . ', ' . $d->format('G:i');
}
}
function formatTimeString($timeStamp) {
$str_time = date("Y-m-d H:i:sP", $timeStamp);
$time = strtotime($str_time);
$d = new DateTime($str_time);
$weekDays = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
$months = ['Jan', 'Feb', 'Mar', 'Apr', ' May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
if ($time > strtotime('-2 minutes')) {
return 'Just now';
} elseif ($time > strtotime('-59 minutes')) {
$min_diff = floor((strtotime('now') - $time) / 60);
return $min_diff . ' min' . (($min_diff != 1) ? "s" : "") . ' ago';
} elseif ($time > strtotime('-23 hours')) {
$hour_diff = floor((strtotime('now') - $time) / (60 * 60));
return $hour_diff . ' hour' . (($hour_diff != 1) ? "s" : "") . ' ago';
} elseif ($time > strtotime('today')) {
return $d->format('G:i');
} elseif ($time > strtotime('yesterday')) {
return 'Yesterday at ' . $d->format('G:i');
} elseif ($time > strtotime('this week')) {
return $weekDays[$d->format('N') - 1] . ' at ' . $d->format('G:i');
} else {
return $d->format('j') . ' ' . $months[$d->format('n') - 1] .
(($d->format('Y') != date("Y")) ? $d->format(' Y') : "") .
' at ' . $d->format('G:i');
}
该函数以时间戳为参数,如果时间戳来自不同的年份,则添加该时间的年份等等操作。
echo date("Y:m:d",strtotime("today"));
echo date("Y:m:d",strtotime("yesterday"));
strtotime
,它是20世纪(或21世纪)最伟大的发明之一:echo strtotime("yesterday"); // midnight
1281391200
echo strtotime("today"); // midnight
1281477600
echo strtotime("today, 1:30");
1281483000
<?php
$date1 = new DateTime("2022-07-26");
$date2 = new DateTime("2022-07-27");
$days = $date2->diff($date1)->format('%r%d');
if($days === 0) {
return 'Today';
} else if($days === "-1") {
return 'yesterday';
}else {
return $days . ' days later';
}
?>
更多细节请参见
something like:
$now = time();
$last_midnight = $now - ($now % (24*60*60));
if ($last_access >= $last_midnight)
{
print "Today";
}
elseif ($last_access >= ($last_midnight-(24*60*60))
{
Print "Yesterday";
}
波斯语类型:
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'سال',
'm' => 'ماه',
'w' => 'هفته',
'd' => 'روز',
'h' => 'ساعت',
'i' => 'دقیقه',
's' => 'ثانیه',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? ' روز' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' قبل' : 'اکنون';
}
这里是可用的函数
function minus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$yesterday = date('Y-m-d',strtotime($date1 . "-1 days"));
return $yesterday; }
希望能为您工作...