Php get start date of month
How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010. Show
S.L. Barth 8,08971 gold badges50 silver badges63 bronze badges asked Apr 21, 2010 at 5:18
1 The easiest way is to use
You can be creative with this. For example, to get the first and last second of a month:
See http://php.net/manual/en/function.date.php for other symbols and more details. answered Jul 23, 2010 at 20:07
Nathan LongNathan Long 120k94 gold badges329 silver badges438 bronze badges 1 Simple one
Reference - http://www.php.net/manual/en/function.date.php
answered Nov 10, 2010 at 5:11
1 You can use the date function to find how many days in a month there are.
Hope that helps. EDIT: After reading Luis' answer, it occurred to me you may want it in the right format (YY-mm-dd). It may be obvious, but doesn't hurt to mention:
answered Apr 21, 2010 at 5:27
Henrik L.Henrik L. 9521 gold badge8 silver badges10 bronze badges This will give you the last day of the month:
And the first Day:
answered Apr 21, 2010 at 5:27
LuisLuis 5,9492 gold badges30 silver badges51 bronze badges 1 For specific month and year use date() as natural language as following
but for Current month
answered Jul 29, 2015 at 5:33
If you want to find the first day and last day from the specified date variable then you can do this like below:
For the current date just simple use this
kelvin 1,3841 gold badge12 silver badges25 bronze badges answered Feb 11, 2017 at 7:56
FaisalFaisal 4,3742 gold badges39 silver badges49 bronze badges 1 To get the first and last date of
Shehary 9,86510 gold badges38 silver badges69 bronze badges answered Jun 18, 2015 at 11:56
karuppubkaruppub 811 silver badge6 bronze badges In simple format
For next month change $curMonth to $curMonth = date('F',strtotime("+1 months")); answered Oct 26, 2017 at 11:13
Yasar ArafathYasar Arafath 5851 gold badge11 silver badges29 bronze badges
display 31 last day of date answered Dec 31, 2015 at 7:42
You can use
answered Jan 21, 2021 at 13:56
For those asking in the mysql context, if you have a $dateFrom like '2022-07-02' and want the first date, do this
You can do similar things for last date etc. answered Jul 28 at 6:48
1 Not the answer you're looking for? Browse other questions tagged php date or ask your own question.How to get 1st date of current month in php?php echo 'First Date = ' . date('Y-m-01') .. Y gives you the 4-digit year from the timestamp ('2010'). m gives you the numeric month from the timestamp, with a leading zero ('04'). t gives you the number of days in the timestamp's month ('30'). How can I get end of month in php?$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );
How can I get last month start and end in php?“get previous month start & end date in php” Code Answer's. $query_date = '2010-02-04';. // First day of the month.. echo date('Y-m-01', strtotime($query_date));. // Last day of the month.. echo date('Y-m-t', strtotime($query_date));. How can get current year start and end date in php?$year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);
|