Create last day of month PHP [duplicate] - php

This question already has answers here:
How to find the last day of the month from date?
(30 answers)
Closed 7 years ago.
I have a form with a date picker which allows the user to select only the month and year.
Upon submitting the form The value it gets from the form is:
$the_Date = $_POST['month_Year'];
echo 'month date: '.$the_Date.'<br />';
// echos 'month date: July 2015' in this example
I need to use this to create 2 dates. I need the 1st of July and the 31st of July.
I can create the 1st no problems using:
$first_Of_Month = date("Y-m-d",strtotime('1 '.$the_Date));
But I don't know how to create the $last_Of_Month seeing as not all months end on the 31st. Any help would be greatly appreciated

When using date(), the value t returns the number of days in the month.
You can run $last_of_month = date("Y-m-t", strtotime($the_Date));

Related

How to get the day of the week in DateTime? [duplicate]

This question already has answers here:
How to find the date of a day of the week from a date using PHP?
(9 answers)
Closed 3 years ago.
How to get the day of the week in DateTime ?
I can’t find the way to get the day of the week from a DateTime.
It’s can be number or string, both are ok.
$today = new DateTime("today");
echo $ today->//whatever to get Friday, Monday or 5, 0
How to get the day of the week in DateTime ?
Edit:
I am using DateTime, not date.
You can use format() function
$today->format('l') //Sunday through Saturday
$today->format('w') //0 (for Sunday) through 6 (for Saturday)

How to add month from current date in PHP [duplicate]

This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 7 years ago.
Hi I'm still new in PHP and currently I want to make system about Employee Allowance and have no idea how to start calculate months in php .For example Steve should get allowance for 4 months from now and database stored the result of calculation, so far the coding is:
?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
Months input is from user and stored in database.
I'm not sure I follow your 40 days versus 4 months, but I think you want something like this:
echo date('Y-m-d', strtotime("Now +40 days"));

Get last date PHP [duplicate]

This question already has answers here:
How can I find the first and last date in a month using PHP?
(12 answers)
Closed 9 years ago.
I want to know how to get LAST DAY when I just know month and year. The month is integer and year is integer too. This is the codes :
<?php
$month = 2;
$year = 15; //I don't know why after "date('y',strtotime($tanggal_awal))" I get 15 not 2015
//how to know last date ????
?>
The real source is not like this. It's very long. I want to know PROPER MANNER TO GET last day WHERE MONTH IS FEBRUARY AND YEAR IS 2015.
You can use the cal_days_in_month() function, the total number of days is the same as what the last day will be.
You may need to install the calendar functions in PHP, follow instructions here: http://www.php.net/manual/en/calendar.installation.php
As suggested in the comments you can also use date('t'), which doesn't require the above extension to be installed.
use this to get the number of days of the desired month...
int cal_days_in_month ( int $calendar , int $month , int $year )
Which gives the last date of the month.
cal_days_in_month

PHP get the date of this weeks monday [duplicate]

This question already has answers here:
Get first day of week in PHP?
(39 answers)
Closed 9 years ago.
Any way to take a date like this 2013-05-29 in PHP and get the date (in the same format) of that weeks monday? So the output would be like this: 2013-05-27
date('Y-m-d', strtotime('last sunday +1 day', strtotime('2013-05-29')));
last sunday +1 day because last monday would return the Monday of the previous week if $timestamp actually was a Monday already.
And parsing of the original date value 2013-05-29 in a second step because all together as one argument does not work well (mixing of absolute and relative date values is something strtotime does not like very much).

Obtain a month is a few days? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP iterate days of month given month and year
"Obtain all days in month with the number of months"
For example i have date 2011/12/16 this date have months 12(december) now i want with code php know that, This month(12 or december) is a few days?(29? or 31? or 30?)
How is it by PHP?
$mydate = strtotime('2011/12/16');
$daysInMonth = date('t',$mydate);
Edited to add strtotime()

Categories