How can i get first day of current month in php?
I can get the Monday of this week with: Show
I would like to get with the same ease the 1st of this month. How can I achieve that?
MSeifert 137k32 gold badges317 silver badges331 bronze badges asked Jan 19, 2010 at 15:53
1 Here is what I use. First day of the month:
Last day of the month:
answered Feb 23, 2012 at 14:17
Etienne DupuisEtienne Dupuis 13.1k6 gold badges46 silver badges56 bronze badges 7 Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:
In PHP 5.4+ you can do this:
If you prefer a concise way to do this, and already have the year and month in numerical values, you can use
N69S 13.8k3 gold badges18 silver badges33 bronze badges answered Jan 19, 2010 at 16:23
John CondeJohn Conde 214k98 gold badges447 silver badges489 bronze badges 1 This is everything you need:
answered Nov 19, 2012 at 22:33
kaleazykaleazy 5,5482 gold badges45 silver badges50 bronze badges 1 Currently I'm using this solution:
The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:
answered Jul 4, 2012 at 7:31
Nikola PetkanskiNikola Petkanski 4,5841 gold badge32 silver badges41 bronze badges 3 I use a crazy way to do this is using this command
Thats all answered Jul 18, 2014 at 16:34
1 In php 5.2 you can use:
answered Jan 20, 2010 at 10:06
pihentagypihentagy 5,7919 gold badges36 silver badges57 bronze badges Ugly, (and doesn't use your method call above) but works:
answered Jan 19, 2010 at 16:21
mr-skmr-sk 12.9k9 gold badges62 silver badges99 bronze badges 3 You can do it like this:
answered Jan 19, 2010 at 16:30
using date method, we should be able to get the result. ie; date('N/D/l', mktime(0, 0, 0, month, day, year)); For Example
answered Jul 20, 2017 at 11:28
Jacob NelsonJacob Nelson 2,18918 silver badges33 bronze badges I use this with a daily cron job to check if I should send an email on the first day of any given month to my affiliates. It's a few more lines than the other answers but solid as a rock.
answered Feb 13, 2019 at 9:01
Craig EdmondsCraig Edmonds 1,9263 gold badges13 silver badges13 bronze badges 2 Timestamp for start of this month and very last second of current month. You can add 00:00:00 or just reference "today" Alternative:
answered May 20, 2021 at 20:46
I am providing this answer as an alternative one liner if the DateTime object is not preferred Basically, I get the current day number, reduce it by one then take that number of days from itself ("today" which automatically resets the clock to 00:00:00 too) and you get the start of the month.
answered Jun 3 at 22:28
If you're using composer, you can install carbon: This is then as simple as:
answered Jun 9 at 9:41
Simon EpskampSimon Epskamp 8,0262 gold badges52 silver badges56 bronze badges All those special php expressions, in spirit of So I decided to build a couple of basic datetime
abstractions and tons of specific implementation which are auto-completed by any IDE. The point is to find what-kind of things. Like, The code in your particular case looks like that:
For more about this approach, take a look at this entry. answered May 9, 2020 at 13:23
Vadim SamokhinVadim Samokhin 3,3044 gold badges39 silver badges66 bronze badges Not the answer you're looking for? Browse other questions tagged php date datetime or ask your own question.How do you get the first day of the month in PHP?php $dt = "2008-02-23"; echo 'First day : '. date("Y-m-01", strtotime($dt)).
How can get first day of current year in PHP?Basically date('Y') returns the value of current year. and the first day of each year starts like 2000-01-01. So $firstdate = date('Y'). ... . I've meant to explain it in your answer... You can edit it anytime! – FZs. ... . for the current year 1st date this will do $startDate= date('Y-01-01'); – Kennedy Maikuma.. How can I get start date and end of month in PHP?You can be creative with this. For example, to get the first and last second of a month: $timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!
How do I get the last day of the current month in PHP?$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );
|