The first day of the current month in php using date_modify as DateTime object
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:
<?php
// First day of this month
$d = new DateTime('first day of this month');
echo $d->format('jS, F Y');
// First day of a specific month
$d = new DateTime('2010-01-19');
$d->modify('first day of this month');
echo $d->format('jS, F Y');
// alternatively...
echo date_create('2010-01-19')
->modify('first day of this month')
->format('jS, F Y');
In PHP 5.4+ you can do this:
<?php
// First day of this month
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2010-01-19'))
->modify('first day of this month')
->format('jS, F Y');
If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date()
:
<?php
echo date('Y-m-01'); // first day of this month
echo "$year-$month-01"; // first day of a month chosen by you
PHP First day of month using DateTime
Just use the ->modify()
method to adjust accordingly:
$datetime = new DateTime();
$datetime->modify('first day of this month');
$output = $datetime->format("w");
echo $output; // 0 - sunday
Without the relative date time string:
$dt = new DateTime();
$dt->setDate($dt->format('Y'), $dt->format('m'), 1);
$output = $dt->format('w');
echo $output;
How to get first day of current month in php?
date('01-m-Y')
should do it ;)
How to find first day of the month with DateTime class
I'm pretty sure the first day of any given month is always numbered 1.. But assuming you want other properties of this month as well; you can use:
$date = new DateTime('2010-09-21');
$date->modify('first day of this month');
echo $date->format('r');
After your update; my answer needs only a small tweak; output $date->format('N');
to get the day-of-the-week number. An overview of the formats you can use is in the documentation
the first day of the month
$j = strtotime("first monday of september 2014");
echo date('d F',$j);
Above code will return result as "01 September".
Try it and let me know if you face any error.
Best Luck
how to get the date of the first day of the month in php
Ok here is my explaination, the php date()
function actually has two inputs but one is optional. The first is the returned format, the second is the time you are converting to a string formatted date. If the second input isn't set it assumes you mean now, so all of these are the same:
date('Y-m-d');
date('Y-m-d',time());
date('Y-m-d',strtotime('now'));
So when you put in date('Y-m-01')
it is pulling the year and month of now but you aren't making the day dynamic, you are forcing it to 01. So in reality here is what you are doing:
$date = date('Y-m') . '-01';
And of course this is server-side so it is based off of the server's clock.
Add +1 or more day to DATETIME in php eaven if that day is in a different month
$date = "2017-12-31";
echo date("Y-m-d", strtotime($date . " +5 days"));
Strtotime() can translate some sentences to actions. See example 1
Get the date for every monday for the current month
You can use relative date/time formats when creating or modifying DateTime objects.
Example:
$date = new DateTime('first Monday of this month');
$thisMonth = $date->format('m');
while ($date->format('m') === $thisMonth) {
echo $date->format('Y-m-d'), "\n";
$date->modify('next Monday');
}
Output:
2015-03-02
2015-03-09
2015-03-16
2015-03-23
2015-03-30
Related Topics
Laravel View Not Found Exception
Why Is PHP Session_Destroy() Not Working
Sorting a Multidimensional Array in PHP
Converting an Integer to a String in PHP
Replace a Whole Line Where a Particular Word Is Found in a Text File
Header Only Retrieval in PHP Via Curl
Check If a User/Guest Has Purchased Specific Products in Woocommerce
Formatting Datetime Object, Respecting Locale::Getdefault()
How to Properly Escape HTML Form Input Default Values in PHP
Woocommerce: Which Hook to Replace Deprecated "Woocommerce_Add_Order_Item_Meta"
The MySQLi Extension Is Missing. Please Check Your PHP Configuration
Why Is Using a MySQL Prepared Statement More Secure Than Using the Common Escape Functions
PHP: Utilizing Exit(); or Die(); After Header("Location: ");