PHP: First Second and Last Second of Last Month

Using PHP, how can I get the timestamp for the first second of last month?
Also, using PHP, how can I get the timestamp for the last second of last month?
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
See the options on this page: http://php.net/manual/en/function.date.php   Note the need to set the default timezone so PHP doesn't complain about it.  See Example #3 to get dates in the past and future.
Brian TaoSenior Business Solutions ConsultantCommented:
<?php
date_default_timezone_set("Canada/Eastern");

// use mktime function: mktime(hour, minute, second, month, day, year)
// it will "fix" invalid numbers, e.g. Oct -1 2015 will become Sep 30 2015, for you
// timestamp for the first second of last month (hour=0, minute=0, second=0, month=<current month - 1>, day=1, year=<current year>)
echo "For the first second of last month: " . "<br>" . PHP_EOL;
$firstSecondLastMonth = mktime(0, 0, 0, date("n")-1, 1, date("Y"));
echo "Timestamp: " . $firstSecondLastMonth . "<br>" . PHP_EOL;
echo "Time: " . date("Y-m-d H:i:s", $firstSecondLastMonth) . "<br>" . PHP_EOL . "<br>" . PHP_EOL;

// timestamp for the last second of last month (hour=0, minute=0, second=-1, month=<current month>, day=1, year=<current year>)
echo "For the last second of last month: " . "<br>" . PHP_EOL;
$lastSecondLastMonth = mktime(0, 0, -1, date("n"), 1, date("Y"));
echo "Timestamp: " . $lastSecondLastMonth . "<br>" . PHP_EOL;
echo "Time: " . date("Y-m-d H:i:s", $lastSecondLastMonth) . "<br>" . PHP_EOL;

/* Output (when run in Oct 2015):
For the first second of last month: 
Timestamp: 1441080000
Time: 2015-09-01 00:00:00

For the last second of last month: 
Timestamp: 1443671999
Time: 2015-09-30 23:59:59
*/
?>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
You can use mktime() or strtotime() to get the answer.  It's all in here.  Read these articles just once and you'll understand PHP date and time, as well has have a bookmark to answers for all future related questions!

Procedural
http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

Object-Oriented:
http://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.