Reputation:
I'm new to php ;) I need to substract two date, stocked in a multidimensionnal array :
45 =>
array (size=2)
'date' => string '2016-01-17 17:08:48' (length=19)
'recall' => string '0' (length=1)
46 =>
array (size=2)
'date' => string '2016-01-17 17:12:43' (length=19)
'recall' => string '5' (length=1)
47 =>
array (size=2)
'date' => string '2016-01-17 17:22:46' (length=19)
'recall' => string '5' (length=1)
I need to Have foreach, date - recall, but i don't understand wich syntax i need to use.. Someone can help the newbie ?
Of Course i tried ;)
$date_array = array();
$date = array();
foreach ( $results as $f ) {
$date_array['date'] = $f['date_to'];
$date_array['recall'] = $f['recall'];
$date[] = $date_array;
}
foreach ($date as $d ) {
$d = date_sub($date['date'],$date['recall']);
$date[] = $d;
}
var_dump($date);
I think i missunderstand how to use datetime.sub
I want to obtain an array similar to :
45 =>
array (size=1)
'date' => string '2016-01-12 17:08:48' (length=19)
46 =>
array (size=1)
'date' => string '2016-01-12 17:12:43' (length=19)
...
Upvotes: 0
Views: 47
Reputation: 2905
PHP date_sub subtracts a period of time from a date. So for example, to get 10 days before a given date. Subtracting a date from another date suggests you want to know the period of time in between those two dates. To do that you can use strtotime(). What this does is converts your date in to a number, in seconds. You can then subtract one from another using normal subtraction.
So it would look something like this:
foreach ($date as $d ) {
$d = strtotime($date['date']) - strtotime($date['recall']);
// Now $d is the number of seconds between recall and date
}
The result is the number of seconds between the two dates - so you can divide it by 60 to get the number of minutes, etc.
Upvotes: 1