mktime function in case of Last day of a month in php - php

Understanding of last day of a month in the PHP function mktime
echo date("Y-m-d H:i:s", mktime(0, 0, 0, 2, 0, 2014));
Output is
2014-01-31 00:00:00
It should be
2014-02-28 00:00:00
Where is the wrong thing I am doing here?

I don't see the problem. It looks correct to me.
You have asked for February 0, which is the day before February 1 AKA Jan 31.

if you set day to 0 it will return the last day of month - 1
<?php
$lastday = mktime(0, 0, 0, 3, 0, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
$lastday = mktime(0, 0, 0, 4, -31, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
?>

echo date("Y-m-d H:i:s", mktime(0, 0, 0, 2, 28, 2014)); //You can't show 29 in feb 2014
OUTPUT : 2014-02-28 00:00:00

Major confusion when you pass the month 4th argument and 5th argument as day = 0 in the mktime method of PHP function.
From PHP official documentation:
Example #3 Last day of a month**
The last day of any given month can be expressed as the "0" day of the next month, not the -1 day. Both of the following examples will produce the string "The last day in Feb 2000 is: 29".
$lastday = mktime(0, 0, 0, 3, 0, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
$lastday = mktime(0, 0, 0, 4, -31, 2000);
echo strftime("\nLast day in Feb 2000 is: %d", $lastday);
// For Feb 2014
$lastday = mktime(0, 0, 0, 3, 0, 2014);
echo strftime("\nLast day in Feb 2014 is: %d", $lastday);
$lastday = mktime(0, 0, 0, 4, -31, 2014);
echo strftime("\nLast day in Feb 2014 is: %d", $lastday);
?>
Output as below:
Last day in Feb 2000 is: 29
Last day in Feb 2000 is: 29
Last day in Feb 2014 is: 28
Last day in Feb 2014 is: 28

Related

Issue while getting start and end date of current Year

I searched a lot on Google to get the start and end date of the current year.
Now, After 2 hours of search, posted the query here.
Below is what i have tried so far. but not working.
$date = date('Y-m-d');
$startDate = new \DateTime($date);
$endDate = new \DateTime($date);
$endDate->modify("+1 day -1 second");
can you please guide me in right direction ?
To get the start date: date('Y-01-01') and end date date('Y-12-31').
If you use Carbon, to get the weekdays:
$year = date('Y');
$date = Carbon::create($year, 12, 31, 0, 0, 0);
$date->toDateTimeString($year, 12, 31, 0, 0, 0); // 2016-12-31 00:00:00
$date->format('l'); // Saturday
$date2 = Carbon::create($year, 1, 1, 0, 0, 0);
$date2->toDateTimeString($year, 1, 1, 0, 0, 0); // 2016-01-01 00:00:00
$date2->format('l'); // Firday

Get start & end timestamp for month from Month and Year

I have following data:
Array
(
[month] => 07
[year] => 2014
)
I want start & end timestamp of july 2014 using above array.
Used functions:
http://php.net//manual/ru/function.cal-days-in-month.php
http://php.net/manual/ru/function.mktime.php
<?php
$array = array('Month' => 07, 'Year' => 2014);
$daysCount = cal_days_in_month(CAL_GREGORIAN, $array['Month'], $array['Year']); //obtain month days count
$firstDay = mktime(0, 0, 0, $array['Month'], 1, $array['Year']); //obtain timestamp of specified month first day
$lastDay = mktime(0, 0, 0, $array['Month'], $daysCount, $array['Year']); //obtain timestamp of specified month last day
echo 'First: '.$firstDay.' Last: '.$lastDay;
?>
Perhaps something like this:
$start_month_str = $array['year'] . '-' . $array['month'] . '-1 midnight';
$end_month_str = $start_month_str . ' + 1 month - 1 minute';
$start_month_stamp = strtotime($start_month_str);
$end_month_stamp = strtotime($end_month_str);
See demo
http://php.net/manual/en/function.strtotime.php

Can't get last day of June with PHP

Comment is the output I'm seeing. Help!
echo date("Y-m-t", strtotime("2012-07-31 -1 month")); //2012-07-31
echo date("Y-m-t", strtotime("2012-07-31 -2 month")); //2012-05-31
July has 31 days, so it can't be processing it as August 1, right? Even if it were, the second line should work. Right?!
Thanks!
What's happening here is, "7-31" minus one month comes out as "6-31", which translates to "7-1", which - asking for the last day of the month - comes out as "7-31".
When you want to do month math, it's almost always, if not always, better to build the time using mktime.
$month = 7;
echo date("Y-m-t", mktime(0, 0, 0, $month - 1, 1, 2012)); //2012-06-30
Note that when doing month math in mktime, always give '1' as the day. Or really anything as long as it's 28 or lower.
You can do something like this:
$month = date('m') ;
$day = date('d') ;
$year = date('Y') ;
// ----
$current = mktime( 0, 0, 0, $month , $day, $year);
$yesterday = mktime( 0, 0, 0, $month , $day-1, $year);
$tomorrow = mktime( 0, 0, 0, $month , $day+1, $year);
// ----
echo '<pre>Current Day: '.date('Y-m-d', $current).'</pre>';
echo '<pre>Day Before: '.date('Y-m-d', $yesterday).'</pre>';
echo '<pre>Day After: '.date('Y-m-d', $tomorrow).'</pre>';
If you're looking for the last day of July:
$july = mktime( 0, 0, 0, 8, 1-1, 2012);
// Last Day of July: 2012-07-31
echo '<pre>Last Day of July: '.date('Y-m-d', $july).'</pre>';
Using mktime, just enter the first day of August and subtract one day.

PHP - If date is less than next January

I'm to write a function that will return a specific day if it's between two dates... I've been trying to use mktime, but it keeps returning December?
Essentially, I'm trying to do this:
$now = date('F d, Y');
if($now [is Between July of last year and January of next year] ) {
//Output last day of January in this year
} elseif($now [is Between January of this year and July of this year]) {
//Output last day of July for next year
}
I'm a little confused on whether I need to be using mktime or strtotime? To determine January of next year, I tried below, but it returned December, 2012?
$jan = date("F,Y", mktime(0, 0, 0, 1, 0, $year+1));
Day 0 of January 2012 is actually December 31st of 2011.
PHP's months are 1-based. Try
$jan = date("F,Y", mktime(0, 0, 0, 1, 1, $year+1));
^--- 1st, not 0th
instead.
The day parameter should be 1 instead of 0. See http://php.net/manual/en/function.mktime.php for details.
date("F,Y", mktime(0, 0, 0, 1, 1, $year+1));
The day param in mktime should be 1 instead of 0:
mktime(0, 0, 0, 1, 1, $year+1);
Otherwise it will think it's "January 0th", which gets translated to "January 1st minus 1 day" = "December 31 from the previous year".
You can actually use this behaviour to add and substract days (or anything really) to dates, like this:
mktime(0, 0, 0, 1, 67, 2012); //returns the correct date for the 67th day of 2012

finding start and end date for 2 conditions

Basically, my question is about finding date-ranges
From today's date [ using date() ], I want to find
starting and ending date for current week
Let's assume
Today: 14 July, 2011
start of week: 10 July, 2011
end of week: 16 July, 2011
starting and ending date for current month
Let's assume
Today: 14 July, 2011
start of month: 1 July, 2011
end of month: 31 July, 2011
How can I find these dates ?
$firstOfTheMonth = date('1 F Y');
$lastOfTheMonth = date('t F Y');
$startOfTheWeek = date('j F Y', mktime(0, 0, 0, date('n'), date('j') - date('w')));
$endOfTheWeek = date('j F Y', mktime(0, 0, 0, date('n'), date('j') - date('w') + 6));
All the different parameters for date basically give you all you need, the rest is a tiny bit of math.

Categories