O
O
OneOlOf2021-07-01 16:41:54
PHP
OneOlOf, 2021-07-01 16:41:54

How to subtract a friend from one date and get values ​​\u200b\u200bin days?

Good afternoon.
I need to subtract from one date to a friend and get the value in days

Now I use this

$now = new DateTime();
    $date = new DateTime("2021-06-01");
    $diff = $date->diff($now)->format("%a");


But it does not return a negative value.

How to make that when we take away for example 2021-06-01 and 2021-07-01 there was a negative value in days?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grisha2217, 2021-07-01
@OneOlOf

First determine which date is newer (by converting to UNIX TIMESTAMP), then subtract the older date from the newer date. Add a minus at the end to the result.

N
Nadim Zakirov, 2021-07-01
@zkrvndm

Read about UNIX time, it will immediately become clear to you how to accomplish your task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question