Bubbleboy
Bubbleboy

Reputation: 71

PHP: time format

i know, it should be very easy, but i have problems with the time functions in PHP. I have a string like "1 h 38 min" and i want to convert it to an integer with the numbers of minutes. Any help or is that to trivial? ;)

Upvotes: 1

Views: 276

Answers (2)

Mark Caudill
Mark Caudill

Reputation: 134

Here is some code that is a bit more robust than sscanf and a bit more specific to using time (and works with older versions of PHP):

$input = '1 d 1 hour 38 min';
$usableInput = trim(str_replace(array(' y ', ' m ', ' d ', ' h ', ' s '), array(' years ', ' months ', ' days ', ' hours ', ' seconds '), " $input "));
$now = time();
$seconds = strtotime("+$usableInput", $now) - $now;
$minutes = floor($seconds / 60);

Upvotes: 0

searlea
searlea

Reputation: 8378

This sounds more like a job for sscanf then any of the time functions.

Something like this:

<?php

$input = '1 h 38 min';
sscanf($input, '%d h %d min', $hours, $minutes);

$minutes += 60 * $hours;

echo "total minutes: $minutes\n";

Upvotes: 4

Related Questions