How to get Time ago and Days left using Php script

How to get Time ago and Days left using Php script

Here i am going to show how to get time ago details and days left details using php script.Time ago is for that activity time, which has been done some time before and the days left function is for that activity time, which is still pending. Here is the how both codes looks.

1.) Time ago function.

function timeAgo($timeAgo)
{
$timeAgo = strtotime($timeAgo);
$curTime = time();
$timeElapsed = $curTime - $timeAgo;
$seconds = $timeElapsed;
$minutes = round($timeElapsed / 60);
$hours = round($timeElapsed / 3600);
$days = round($timeElapsed / 86400);
$weeks = round($timeElapsed / 604800);
$months = round($timeElapsed / 2600640);
$years = round($timeElapsed / 31207680);
/* Seconds  Calculation*/
if ($seconds <=60) {
return'Just Now';
} /* Minutes */
elseif ($minutes <=60) {
if ($minutes ==1) {
return"One Minute ago";
} else {
return $minutes." time ago minutes";
}
} /* Hours */
elseif ($hours <=24) {
if ($hours ==1) {
return"One hour ago";
} else {
return $hours." Hour Ago";
}
} /* Days */
elseif ($days <=7) {
if ($days ==1) {
return"One day ago";
} else {
return $days." days ago";
}
} /* Weeks */
elseif ($weeks <=4.3) {
if ($weeks ==1) {
return"One week ago";
} else {
return $weeks." week ago";
}
} /* Months */
elseif ($months <=12) {
if ($months ==1) {
return"One month ago";
} else {
return $months." month ago";
}
} /* Years */
else {
if ($years ==1) {
return"One year ago";
} else {
return $years." year ago";
}
}
}

2.) Days left function.

function daysleft($time)
{
$result = null;
$toDate = strtotime($time);  /*Future date*/.
$curDate = strtotime(date('Y-m-d'));
$timeleft = $toDate - $curDate;
$daysleft = round((($timeleft / 24) / 60) / 60);
if ($daysleft ==1) {
$result = $daysleft . ' ' . "Day" . ' ' . "left";
} else if ($daysleft > 1) {
$result = $daysleft . ' ' . "Day" . ' ' . "left";
} else if ($daysleft == -1) {
$result = $daysleft . ' ' . "Day" . ' ' . "Gone";
} else if ($daysleft > -1) {
$result = $daysleft . ' ' . "Day" . ' ' . "Gone";
}
return $result;
}

Note: Set date_default_timezone_set() before you using both functions so that you will get correct time information.Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

php Data Types – II

PHP as Array

An array is used to store multiple values in a single variable.

LET’S SEE WITH AN EXAMPLE:-

<?php 
$techalltype= array("html","php","dotnet");
var_dump($cars);
?>

OUTPUT:-

array(3) { [0]=> string(4) “html” [1]=> string(3) “php” [2]=> string(6) “dotnet” }

explanation:-

array(3) – it means array contain 3 variables

[0]=> string(4) “html” – it means at  index [0]=> string(4) variable length is 4, and that is “html”.

Note:- An array count the variable from index[0].

PHP as Object:- 

An object is stores data and information to process that data.

In order to use php as object we need to declare a class of object, by class keyword. Then use class as a structure which can contain properties and methods.

Let’s see with an example:-

<?php
class techall {
function techall() {
$this->techtype = "TECHALLTYPE";
}
}

$alltechtype = new techall();     // create an object by using new keyword


echo $alltechtype->techtype; // Display object properties
?>

Output:-

TECHALLTYPE

PHP NULL Value:-

Null is a data type which can have only one and that is NULL value, no other value assigned to it.

Let’s see with an example:-

<?php
$x = "Welcome to Tech all type";
$x = null; // $x now assigned Null Value
var_dump($x);
?>

Output:-

NULL