Get last date PHP [duplicate] - php

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

Related

Get the first and last day of the workweek when a date is given [duplicate]

This question already has answers here:
Get first/last day of week in php?
(4 answers)
Closed 3 years ago.
I am trying to get the first day of the week(Monday) and the last day of the workweek(Friday) when I give a date. Normally the date that is given is the date of Monday but there are exceptions.
The date given is for example 2019-04-18.
The last day of the workweek here is 2019-4-19 and the first is 2019-4-15.
I'm working in laravel and would like to use it in a blade template so if possible I would like to do it in like one line of code if possible.
I am able to get the last day of the workweek but still have trouble with the first day. I am also not certain that the way I get the last day is the best.
{{ date("d-m-Y", strtotime(date('Y-m-d', strtotime(($overtime->workdate). ' + '.(5-date('w', strtotime($overtime->workdate))).' days')))) }}
The problem with the answers that have been given is that I am trying to get it all in one line in a blade template so I cannot make a new DateTime or something. It starts with a string that gives a date.
You can use carbon to achieve this,
$date = new Carbon('2019-04-18'); //
// To get the first week of the day we can do this
echo $date->startOfWeek(); // 2019-04-15
echo $date->endOfWeek(); // 2019-04-19
If you are unaware of Carbon, here is the doc to help you out.

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)

PHP CreateFromFromFormat() Month & Year Only [duplicate]

This question already has an answer here:
date_create_from_format() returns wrong value. [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to create data from format using Datetime Class as following
$date = DateTime::createFromFormat('m-Y', '02-2016');
echo $date->format('Y-m-d');
Out put 2016-03-01, i was expecting to get 2016-02-01
is it a bug? or i am understating this function in a wrong way?
If you provide month and year, then PHP will provide a default day, using the current day of the current month (ie 30) today
That gives an effective 2016-02-30, which isn't a valid date..... but PHP allows these types of values where days is higher/lower than days in month, months are higher/lower than months in the year, and just increments/decrements to a valid date..... in this case, 1 additional day after the last day in month 2 or 2016 (29th February 2016) to give 1st March 2016
EDIT
Reference for day/month overflow/underflow behaviour
Note:
It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).
It is also possible to underflow the mm and MM formats with the value 0. A month value of 0 means December of the previous year. As example "2008-00-22" is equivalent to "2007-12-22".
If you combine the previous two facts and underflow both the day and the month, the following happens: "2008-00-00" first gets converted to "2007-12-00" which then gets converted to "2007-11-30". This also happens with the string "0000-00-00", which gets transformed into "-0001-11-30" (the year -1 in the ISO 8601 calendar, which is 2 BC in the proleptic Gregorian calendar).
It's an known bug... or a documented feature, depending on how you look at it. See Mark Baker's answer for more details.
Workaround :
To get the date you expect, just add 01 to your input as a value for the day of the month.
$input = '02-2016';
$date = DateTime::createFromFormat('d-m-Y', '01-' . $input);
echo $date->format('Y-m-d');
// OUTPUT : 2016-02-01

Create last day of month PHP [duplicate]

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));

calculating calendar week php [duplicate]

This question already has an answer here:
Week number and Week day
(1 answer)
Closed 9 years ago.
If anyone has an idea how can I calculate a calendar week depending on given date in php, for example 06.06.2013 (format like this 2013-06-06). is calendar week 23. I was searching for the solution all morning and found nothing useful. Any help or link, anything would be appreciated. Thank you
Use date("W")
echo date("W", strtotime('2013-06-06'));
See it in action
date('W') should give you the week of the year. RTM
If you don't have your time as a unix timestamp you can use strtotime() first and pass it as the second parameter
ex. date('W',strtotime($my_time_string))
You can see the PHP manual here
Like this:
$time = '2013-06-06';
$calendar_week = date('W', strtotime($time));

Categories