➩ subtract 2 dates in PHP and show the days results tutorial and free code

# ➩ subtract 2 dates in PHP and show the days results tutorial and free code

## show the days results between 2 dates in PHP code tutorial and free code | difference between 2 dates subtract 2 dates in PHP and show the days results tutorial and free code

when the programmers in a php project we must use a function in php where you can show the days subtracted between 2 dates in this case we will take an old date: 11/05/2018 where this post was written and the date of today , we will use format which will give us the solution to know the days.

First we will change the old date as DateTime, to be able to use the function and subtract the days, but we will first use the function of php time (), in the following way:

``````
date_default_timezone_set('America/new_york');
\$time = time();
\$dia1 = date("d", \$time);
\$mes1 = date("m", \$time);
\$ano1 = date("Y", \$time);
\$hhorao = date("h", \$time);
\$fechadehoy = \$ano1 . '-' . \$mes1 . '-' . \$dia1;
``````

where \$fechadehoy in php date takes today's exact date in both days, months and year, date_default_timezone_set takes the local time on the server at the time it is captured.

then we use a function called restardias which will return the days subtracted between these 2 dates:

``````
function restardias(\$datetime1, \$datetime2) {
// echo '' . \$datetime1 . ' ' . \$datetime2;
\$interval = \$datetime1->diff(\$datetime2);
\$restar = \$interval->format('%R%a');
return \$restar;
}
``````

In the old date we save it in a variable \$fecha_ temp = "05/11/2018"; finally I will leave the complete code calling the function and showing the days subtracted with echo:

``````

<?php
date_default_timezone_set('America/new_york');

function restardias(\$datetime1, \$datetime2) {
// echo '' . \$datetime1 . ' ' . \$datetime2;
\$interval = \$datetime1->diff(\$datetime2);
\$restar = \$interval->format('%R%a');
return \$restar;
}
\$time = time();
\$dia1 = date("d", \$time);
\$mes1 = date("m", \$time);
\$ano1 = date("Y", \$time);
\$hhorao = date("h", \$time);
\$fechadehoy = \$ano1 . '-' . \$mes1 . '-' . \$dia1;
\$fecha_temp="11/05/2018";
{"ft_lang_label":"__label__en","ft_lang_prob":0.6125969,"math_prob":0.984153,"size":2289,"snap":"2021-43-2021-49","text_gpt3_token_len":632,"char_repetition_ratio":0.15142232,"word_repetition_ratio":0.24590164,"special_character_ratio":0.31323722,"punctuation_ratio":0.15981735,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.992102,"pos_list":[0,1,2],"im_url_duplicate_count":[null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-01T03:53:31Z\",\"WARC-Record-ID\":\"<urn:uuid:cd97ed9c-a676-4dc1-8c14-91d82f9010d5>\",\"Content-Length\":\"279910\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ed373559-b62d-4fff-ac80-cdc743a0f12e>\",\"WARC-Concurrent-To\":\"<urn:uuid:1fdfe614-9119-4ad4-ada6-2d34defa4c3a>\",\"WARC-IP-Address\":\"142.250.65.83\",\"WARC-Target-URI\":\"https://www.mbajava.com/2018/11/subtract-2-dates-in-php.html\",\"WARC-Payload-Digest\":\"sha1:KXBBBILNLWY5SQLO7VD3RGEZYN3RUEPL\",\"WARC-Block-Digest\":\"sha1:5TFX3RFPKIYSIHBTCNNW4XIXXBRJURQF\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964359082.78_warc_CC-MAIN-20211201022332-20211201052332-00519.warc.gz\"}"}