D
D
DashBerlin2018-02-13 10:32:12
PHP
DashBerlin, 2018-02-13 10:32:12

How to calculate time?

There is a time, for example, in the format "2018-01-22 20:49:46" and there is a current time, how to calculate how much time has passed using php:
date - the current time is less than an hour, how many minutes have passed?
date - current time is less than 6 hours, how many minutes have passed?
date - current time - today
date - current time - yesterday

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Crash, 2018-02-13
@Bandicoot

You need to translate the desired time and the current time into unixtime, it will not be difficult to calculate the difference
php.net/manual/en/function.strtotime.php

D
Dmitry, 2018-02-13
@slo_nik

Good morning.
Here is a small example for you on how to calculate the difference between dates.

$first = strtotime('04-02-2017 06:30');
$second = strtotime('14-03-2017 10:30');

$start = new DateTime(date('Y-m-d H:i', $first), new DateTimeZone('Europe/Moscow'));
$end = new DateTime(date('Y-m-d H:i', $second), new DateTimeZone('Europe/Moscow'));
$diff = $end->diff($start);

printf("The two dates have %d weeks, %s days, " .
"%d hours, %d minutes, and %d seconds " .
"elapsed between them.",
floor($diff->format('%a') / 7),
$diff->format('%a') % 7,
$diff->format('%h'),
$diff->format('%i'),
$diff->format('%s'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question