jason white
jason white

Reputation: 711

PHP finding the expired time in seconds

Let say we have start date and end date

 if (isset($_POST['start_date']))
    $_POST['start_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['start_date']));

 if (isset($_POST['end_date']))
    $_POST['end_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['end_date']));

would $_POST['end_date'] - $_POST['start_date'] give you the expired time in seconds?

Upvotes: 1

Views: 129

Answers (2)

rationalboss
rationalboss

Reputation: 5389

No, to get the expired time in seconds you would use strtotime():

$expired = strtotime($_POST['end_date'])-strtotime($_POST['start_date']);

if (isset($_POST['start_date']))
    $_POST['start_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['start_date']));

if (isset($_POST['end_date']))
    $_POST['end_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['end_date']));

if (isset($_POST['start_date']) && isset($_POST['end_date'])) echo 'Expired time in seconds: ' . $expired;

Upvotes: 3

Radoslav Georgiev
Radoslav Georgiev

Reputation: 1376

I suggest you to use mktime function to get the unix timestamp from a date literal, which is in milliseconds.

$startDate = mktime($hour, $minute, $second, $month, $day, $year);
$endDate = mktime($hour, $minute, $second, $month, $day, $year);

$diffInSeconds = ($endDate - $startDate) / 1000; // 1 second = 1000 miliseconds

Upvotes: -1

Related Questions