我在实施TIME AGO日期时遇到了问题。代码我要么显示“2年前”...如果我尝试从我的数据库添加一个时间戳,它会让我“46年前。我真的迷失了。我一直在使用”新的DateTime(); “,time(),date(),没有任何工作。字符串应该是对象,不正确的变量等等。 这是代码......
<?php
function timeAgo($time_ago){
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
echo "one minute ago";
}
else{
echo "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
echo "an hour ago";
}else{
echo "$hours hours ago";
}
}
//Days
else if($days <= 7){
if($days==1){
echo "yesterday";
}else{
echo "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
echo "a week ago";
}else{
echo "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
echo "a month ago";
}else{
echo "$months months ago";
}
}
//Years
else{
if($years==1){
echo "one year ago";
}else{
echo "$years years ago";
}
}
}
?>
电话是......
<?php
$curenttime="2013-07-10 09:09:09";
$time_ago =strtotime($curenttime);
echo timeAgo($time_ago);
?>
我的时间戳的SQL字符串是$r->time
任何帮助都会有用。该代码将在http://autodude666.com/network的无线电请求页面中使用。 非常感谢提前
数据库查询...
<?php
//error_reporting(0);
require '../db/connect_guest.php';
require '../db/security.php';
$records = array();
if($results = $db->query("SELECT * FROM guests ORDER BY id DESC")){
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
echo '<pre>', print_r($records), '</pre>';
?>
数据库插入是....
if(!empty($_POST)){
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$time = date("g:i A");
$date = date("Y-m-d H:i:s");
$ip = $_SERVER['REMOTE_ADDR'];
if(!empty($name) && !empty($message)){
$insert = $db->prepare("INSERT INTO guests (name, email, message, date, time, ip) VALUES (?, ?, ?, ?, ?, ?)");
$insert->bind_param('ssssss', $name, $email, $message, $time, $date, $ip);
if($insert->execute()){
header('location: index_input.php');
die();
}
}
}
}
答案 0 :(得分:2)
我猜您需要添加date_default_timezone_set('TimeZone here');
,以便返回正确的日期。
答案 1 :(得分:0)
您需要使用intval()
代替round()
更改:
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
到
$minutes = intval($time_elapsed / 60 );
$hours = intval($time_elapsed / 3600);
$days = intval($time_elapsed / 86400 );
$weeks = intval($time_elapsed / 604800);
$months = intval($time_elapsed / 2600640 );
$years = intval($time_elapsed / 31207680 );
答案 2 :(得分:0)
<?php
function timeAgo($time_ago){
$cur_time = new DateTime("now");
$time_ago = new DateTime($time_ago);
$time_elapsed = $cur_time->diff($time_ago, true);
$seconds = $time_elapsed->s;
$minutes = $time_elapsed->i;
$hours = $time_elapsed->h;
$days = $time_elapsed->d;
// $weeks = diff function doesn't give weeks in the object.
$months = $time_elapsed->m;
$years = $time_elapsed->y
echo $years." years, ".$months." months, ".$days." days and ".$hours." hours ".$minutes." minutes and ".$seconds." seconds ago.";
}
?>
从那里玩它......
其他一些附注:
在数据库中插入if(isset($_POST['name'], $_POST['email'], $_POST['message']))
Post会发送一个空字段(即如果他们不填写姓名或电子邮件)作为空白字符串,isset将评估为true(它被设置为空字符串。)这意味着这些将永远是真的,所以它是一个毫无价值的测试。请改用空。
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {
您还可以使用时间戳将时间和日期的数据库表列合并到一个列中,然后在每次拉动时将其转换为您想要的任何格式。