Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get seconds until the end of the month in PHP

Tags:

php

I would like to set a cookie with PHP that has to expire at the end of the month.

How can I get the number of seconds until the end of the month?

Thank you.

like image 455
Psyche Avatar asked Jan 20 '26 01:01

Psyche


1 Answers

You can use time() to get the number of seconds elapsed since the epoche. Then use strtotime("date") to get the number of seconds to your date. Subtract the two and you have the number of seconds difference.

This will give you the last second of the month:

$end = strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')) - 1;

This will give you now:

$now = time();

This will give you the distance:

$numSecondsUntilEnd = $end - $now;
like image 133
Malfist Avatar answered Jan 21 '26 14:01

Malfist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!