PHP Gets the current month with the month and month-end timestamp method _php Tips

Source: Internet
Author: User
Tags php programming php regular expression

This article describes how PHP obtains the current month and the month and month-end timestamp. Share to everyone for your reference, specific as follows:

Current month

<?php
$thismonth = date (' m ');
$thisyear = Date (' Y ');
$startDay = $thisyear. '-' . $thismonth. '-1 ';
$endDay = $thisyear. '-' . $thismonth. '-' . Date (' t ', Strtotime ($startDay));
$b _time  = Strtotime ($startDay);//The beginning of the current month timestamp
$e _time  = Strtotime ($endDay);//month-end timestamp of the current month

Last month

<?php
$thismonth = date (' m ');
$thisyear = Date (' Y ');
if ($thismonth = = 1) {
 $lastmonth =;
 $lastyear = $thisyear-1;
} else {
 $lastmonth = $thismonth-1;
 $lastyear = $thisyear;
}
$lastStartDay = $lastyear. '-' . $lastmonth. '-1 ';
$lastEndDay = $lastyear. '-' . $lastmonth. '-' . Date (' t ', Strtotime ($lastStartDay));
$b _time = Strtotime ($lastStartDay); time stamp
$e _time = Strtotime ($lastEndDay) last month;/month

The key here is the T in the Date function, which is used to get the number of days that the current month contains, 28 days, 29 days, 30 days, 31 days. How many days, the month is the number.

PS: This site also provides a UNIX timestamp conversion tool, including a variety of common language for time stamping operation methods, provided to you for reference:

Unix timestamp (timestamp) conversion tool:
Http://tools.jb51.net/code/unixtime

More about PHP Interested readers can view the site topics: "PHP date and Time usage summary", "PHP Mathematical Calculation Skills Summary", "PHP Array" operation Skills Encyclopedia, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Regular Expression Usage summary, "Summary of PHP operations and operator usage", "PHP string (String) Usage summary" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.