rails_noob
rails_noob

Reputation: 311

How to print time remaining

enter image description here

Here is my table. dateline column is in hours.

How can I print time remaining in hours based on dateline, created_at and current time?

Upvotes: 1

Views: 232

Answers (2)

Jared Farrish
Jared Farrish

Reputation: 49208

You can use strtotime():

<?php

$created = 1323504315;
$timeleft = 144;
$expires = strtotime("+$timeleft hours", $created);

$compare_text = "created: " . date('m/d/Y H:i A',$created) . ", now: " . date('m/d/Y H:i A') . " > expires:" . date('m/d/Y H:i A',$expires);

if (mktime() > $expires) {
    echo "The time has expired. ($timeleft, $compare_text)";
} else {
    echo "The time has not expired. ($timeleft, $compare_text)";
}

?>

http://codepad.org/WcCObV1z

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

$hours = "144"; //from dataline
$totalHours = date("H") - $hours;

$totalMinutes = date("i");

$totalSeconds = date("s");

$totalMonths = date("m");

$totalDays = date("d");

$totalYears = date("Y");

$timeStamp = mktime($totalHours, $totalMinutes, $totalSeconds, $totalMonths, $totalDays, $totalYears);

$myTime = date("H:i A", $timeStamp);

Upvotes: 0

Related Questions