Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a PHP time class
    primarykey
    data
    text
    <p>I want to make a PHP class that can collect information from the database and checks when that particular operation was performed, based on that it should be able to tell me that the operation was performed 10 minutes ago, or 2 days ago, or 3 years ago, I made a PHP class but sometimes it returns negative time, and I checked and cross checked it did not seem as if anything was wrong, please I need help</p> <pre><code>&lt;?php class ClassTimeFormat{ protected $dateTime; public function __construct($dateTime){ $this-&gt;dateTime = $dateTime; } public function getYears(){ $sql= " SELECT YEAR('$this-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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') &gt; $this-&gt;getYears()){ return (date('Y') - $this-&gt;getYears())." Years ago"; } else{ if($this-&gt;getMonth() != date('n')){ return (date('n') - $this-&gt;getMonth())." Months ago"; } else{ if($this-&gt;getDayofMonth() != date('j')){ return (date('j') - $this-&gt;getDayofMonth())." Days ago"; } else{ if($this-&gt;getHour() != date('G')){ return (date('j') - $this-&gt;getHour())." Hours ago"; } else{ if($this-&gt;getMins() != date('i')){ return (date('i') - $this-&gt;getMins())." Minutes ago"; } else{ if($this-&gt;getSecs() != date('s')){ return (date('s') - $this-&gt;getSecs())." Seconds ago"; } else{ return "Just now"; } } } } } } } } </code></pre> <p>?></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload