我正在使用聊天应用程序,我需要显示类似于WhatsApp的时间格式,我想使用Carbon库来执行此操作,在该库中输入日期时间应该与我使用相同的格式。
我需要这样的输出。
getTime()
如果您看到上面的WhatsApp联系人列表以黄色突出显示了时间,星期几和日期。我需要相同的格式。
public static function getTime($getdate){
$today = Carbon::now();
$inputDate = Carbon::parse($getdate);
$start_date = $today->copy()->startOfWeek()->subDays(9);
$end_date = $today->copy()->endOfWeek()->subDays(9);
$formatDate1 = $today->format('Y/m/d');
$formatDate2 = $inputDate->format('Y/m/d');
switch(TRUE) {
case ($today->diffInDays($inputDate) === 0):
$time = $inputDate->format('g:i A');
break;
case ($today->diffInDays($inputDate) === 1):
$time = 'Yesterday';
break;
case ($inputDate->between($start_date,$end_date)):
$time = $inputDate->format('l');
break;
default:
$time = $inputDate->format('Y/m/d');
}
return $time;}
示例:
getTime('2019-05-24 00:00:00') // Friday
getTime('2019-05-26 00:00:00') // Yesterday
getTime('2019-05-27 00:00:00') // 12:34 AM
getTime('2019-05-15 00:00:00') // 2019/05/15
答案 0 :(得分:0)
我自己使用carbon库创建了该库。并且此脚本将返回与您在我上面的问题图片中所看到的消息相同的格式。我将以相同的时间模式。只需输入联系日期。像这样getTime($contact['created_at'])
,您将获得所需的输出。 2019-05-24 00:00:00
希望其他人也会这样做。
public static function getTime($getdate){
$today = Carbon::now();
$inputDate = Carbon::parse($getdate);
$start_date = $today->copy()->startOfWeek()->subDays(9);
$end_date = $today->copy()->endOfWeek()->subDays(9);
$formatDate1 = $today->format('Y/m/d');
$formatDate2 = $inputDate->format('Y/m/d');
switch(TRUE) {
case ($today->diffInDays($inputDate) === 0 && $formatDate1 == $formatDate2):
$time = $inputDate->format('g:i A');
break;
case ($today->diffInDays($inputDate) === 1 || ($today->diffInDays($inputDate) === 0 && $formatDate1 > $formatDate2)):
$time = 'Yesterday';
break;
case ($inputDate->between($start_date,$end_date)):
$time = $inputDate->format('l');
break;
default:
$time = $inputDate->format('Y/m/d');
}
return $time;
}
示例:
getTime('2019-05-24 00:00:00') // Friday
getTime('2019-05-26 00:00:00') // Yesterday
getTime('2019-05-27 00:00:00') // 12:34 AM
getTime('2019-05-15 00:00:00') // 2019/05/15