我想创建一个 PHP 类,它可以从数据库收集信息并检查执行该特定操作的时间,基于它应该能够告诉我该操作是在 10 分钟前、2 天前或 3 年前执行的以前,我做了一个 PHP 类,但有时它返回负时间,我检查并交叉检查它似乎没有任何问题,请我需要帮助
<?php
class ClassTimeFormat{
protected $dateTime;
public function __construct($dateTime){
$this->dateTime = $dateTime;
}
public function getYears(){
$sql= " SELECT YEAR('$this->dateTime') as year";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result) or die(mysql_error());
return $row['year'];
}
public function getDayofMonth(){
$sql = "SELECT DAY('$this->dateTime') as day";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['day'];
}
public function getDayofWeek(){
$sql = "SELECT DAYNAME('$this->dateTime') as dayofweek";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['dayofweek'];
}
public function getMonthName(){
$sql = "SELECT MONTHNAME('$this->dateTime') as monthname";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['monthname'];
}
public function getMonth(){
$sql = "SELECT MONTH('$this->dateTime') as month";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['month'];
}
//TIme methods
public function getSecs(){
$sql= " SELECT SECOND('$this->dateTime') as seconds";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['seconds'];
}
public function getMins(){
$sql= " SELECT MINUTE('$this->dateTime') as minutes";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['minutes'];
}
public function getHour(){
$sql= " SELECT HOUR('$this->dateTime') as hours";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['hours'];
}
public function getSecsDiff(){
$sql= " SELECT EXTRACT(SECOND FROM NOW())- EXTRACT(SECOND FROM '$this->dateTime') as seconds;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['seconds'];
}
public function getMinsDiff(){
$sql= " SELECT EXTRACT(MINUTE FROM NOW())- EXTRACT(MINUTE FROM '$this->dateTime') as minutes;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['minutes'];
}
public function getHourDiff(){
$sql= " SELECT EXTRACT(HOUR FROM NOW()) - EXTRACT(HOUR FROM '$this->dateTime') as hours;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
return $row['hours'];
}
public function getExactTime(){
if(date('Y') > $this->getYears()){
return (date('Y') - $this->getYears())." Years ago";
}
else{
if($this->getMonth() != date('n')){
return (date('n') - $this->getMonth())." Months ago";
}
else{
if($this->getDayofMonth() != date('j')){
return (date('j') - $this->getDayofMonth())." Days ago";
}
else{
if($this->getHour() != date('G')){
return (date('j') - $this->getHour())." Hours ago";
}
else{
if($this->getMins() != date('i')){
return (date('i') - $this->getMins())." Minutes ago";
}
else{
if($this->getSecs() != date('s')){
return (date('s') - $this->getSecs())." Seconds ago";
}
else{
return "Just now";
}
}
}
}
}
}
}
}
?>