Php 你好,我';我试图打印从mysql时间戳发布以来经过的时间
它将输出以下内容: 公开共享-$分钟前您的问题是Php 你好,我';我试图打印从mysql时间戳发布以来经过的时间,php,mysql,Php,Mysql,它将输出以下内容: 公开共享-$分钟前您的问题是 <?php date_default_timezone_set('Africa/Harare'); function get_timeago($connect, $post_datetime){ $query = "SELECT * FROM posts WHERE post_datetime = :post_datetime"; $statement = $connect->prepare($query);
<?php
date_default_timezone_set('Africa/Harare');
function get_timeago($connect, $post_datetime){
$query = "SELECT * FROM posts WHERE post_datetime = :post_datetime";
$statement = $connect->prepare($query);
$statement->execute(
array(
':post_datetime' => $post_datetime
)
);
$timeago = $post_datetime;
$current_time = time();
$time_difference = $current_time - $timeago;
$seconds = $time_difference;
$minutes = round($seconds / 60);
$hours = round($seconds / 3600);
$days = round($seconds / 86400);
$weeks = round($seconds / 604800);
$months = round($seconds / 2629800); //((365 + 365 + 365 + 366)/4/12) * 24 * 3600
$years = round($seconds / 31557600); //$months * 12
if($seconds <= 60){
return 'Just Now';
} else if($minutes <= 60){
if($minutes == 1){
return 'a minute ago';
} else {
return '$minutes minutes ago';
}
} else if($hours <= 60){
if($hours == 1){
return 'an hour ago';
} else {
return '$hours hrs ago';
}
} else if($days <= 7){
if($days == 1){
return 'Yesterday';
} else {
return '$days days ago';
}
} else if($weeks <= 4){
if($weeks == 1){
return 'a week ago';
} else {
return '$weeks weeks ago';
}
} else if($months <= 12){
if($months == 1){
return 'a month ago';
} else {
return '$months months ago';
}
} else if($years == 1){
return 'a year ago';
} else {
return '$years years ago';
}
return $result;
}
<span class="description">Shared publicly <i class="fa fa-globe"></i> - '.get_timeago($connect, strtotime($row['post_datetime'])).'</span>
?>
好吧,反正是其中的一部分。在PHP中,单引号不插入变量,它们不会被替换,而是作为字符串文本处理
请改为使用“
双引号
'$years years ago'
它将输出以下内容:
哪一位是?我猜这一位是公开共享的-$minutes-minutes-ago
双引号将扩展$vars,但单引号不会如此返回“$weeks-before”";
PS:返回$result代码>???什么$result
??我认为函数必须返回一些东西,这就是为什么我将查询执行存储在$result变量中。我想它应该还些什么,但不知道怎么回事,谢谢你们。让我照你说的做。非常感谢您的帮助。这是一个很容易犯的错误。谢谢@ArtisticPhoenix的帮助。将单引号改为双引号,现在工作正常。
$foo = 'replaced';
echo "$foo"; //outputs replaced
echo '$foo'; //outputs $foo