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
Related
EDITED:
THIS PART IS WHAT I NEED TO SOLVE:
I have a negative integer like: -10800 and I wish to convert this at my local time, which is Buenos Aires (gmt -3).
I'm using an online converter to check what date it is so I can check the results: www.4webhelp.net and it throws that -10800 is Wednesday, December 31st 1969, 18:00:00 (GMT -3) but I believe this is not correct. I was told the negative number is in my timezone which is Buenos Aires.
I also want to create from date to string using the same time format. How can I do this?
THIS PART IS SOLVED:
Among the things I tryied in order to do this, a new doubt came up when learning about mktime:
I was trying to know which is the last day for february:
$lastday = mktime(0, 0, 0, 2, 0, 2015);
echo strftime("Last day is: %d", $lastday);
And this throws that february has 31 days which is incorrect. Why is that so?
If day is 0, then PHP takes the last day of the previous month, so
$lastday = mktime(0, 0, 0, 2, 0, 2015);
Gives 31st January 2015
(0th day of month 2, effectively)
As per the PHP docs
day
The number of the day relative to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
(my emphasis)
EDIT
If you want the last day of February, use
$lastday = mktime(0, 0, 0, 2, 1, 2015);
echo 'Last day is: ', date("t", $lastday);
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
Is there a way that I can have mktime go back a certain amount of months based of the first day of the month rather than the current day? Right now my code is echoing March instead of February because there is no 29 in February.
date("F Y", mktime(0, 0, 0, date('m') - 8, date('d'), date('Y')))
Just set the "day" parameter to 1:
date("F Y", mktime(0, 0, 0, date('m') - 8, 1, date('Y')))
How can I get last year's start and end date using PHP code? Is it possible?
The first day is always January 1, the last day is always December 31. You're really only changing the year attached to it. Depending on how you want the date formatted, you have a couple possibilities...
If you just want to display the physical date:
$year = date('Y') - 1; // Get current year and subtract 1
$start = "January 1st, {$year}";
$end = "December 31st, {$year}";
If you need the timestamp for both those dates:
$year = date('Y') - 1; // Get current year and subtract 1
$start = mktime(0, 0, 0, 1, 1, $year);
$end = mktime(0, 0, 0, 12, 31, $year);
Very simple stuff. You can manually specify which year if you wanted too. The premise is the same.
You can do it by using the below. Hope it helps someone.
//to get start date of previous year
echo date("d-m-y",strtotime("last year January 1st"));
//to get end date of previous year
echo date("d-m-y",strtotime("last year December 31st"));
start date of the year :
mktime(0,0,0,1,1,$year);
end date of the year :
mktime(0,0,0,1,0,$year+1);
Check this Stuff
$currentY = date('Y');
$lastyearS = mktime(0, 0, 0, 1, 1, $currentY-1 )."<br/>";
$lastyearE = mktime(0, 0, 0, 12, 31, $currentY-1 )."<br/>";
echo date('Y-m-d',$lastyearS)."<br/>";echo date('Y-m-d',$lastyearE);
Suppose if your current month is February or the month which has 30 days
echo date('Y-12-t', strtotime(date('Y-m-d'))); // if current month is february (2015-02-01) than it gives 2015-02-28
will give you inaccurate results
Solution:
So to get accurate result for the end date of an year, try the code below
$start_date = date("Y-01-01", strtotime("-1 year"));// get start date from here
$end_date = date("Y-12-t", strtotime($start_date));
(OR)
$last_year_last_month_date = date("Y-12-01", strtotime("-1 year"));
$end_date = date("Y-12-t", strtotime($last_year_last_month_date));
I am using a unix time stamp as the base of my starting date for use in a date ranged query. The start date is not the problem, for the purpose of this example i will use the following time stamp: 1228089600 (01 December 2008 00:00:00).
For use in my query I needed to easily figure out the last day of any given month to the last second so..
date('o-m-d G:i:s',mktime(0, 0, -1, date("m",1228089600)+1, 1, date("o",1228089600)));
This method has been working fine for me for every other month except December.. By taking the start date, adding a whole month (01 January 2009 00:00:00) then taking away 1 second I was expecting to result in the date I required (31 December 2008 23:59:59). However it appears the year is being calculated correctly for the additional month, but not for the subtracting second as the date returned is 31 December 2009 23:59:59.
As I say, this method has worked great until I discovered this problem. But it's a problem I am unable to figure out the cause of or simple solution to..
Any help is greatly appreciated.
This happens because:
flag 'o' - ISO-8601 year number. This has the
same value as Y, except that if the
ISO week number (W) belongs to the
previous or next year, that year is
used instead. (added in PHP 5.1.0)
So in your case happens this:
Date generated is 1st January 2009
Date is decremented with 1 second (so you get 31st December 2008)
But as goes the description above - the week belongs to 2009 so 2009 is returned instead of 2008
(belongs to year means: More days of the week are in 2009 than in 2008 - in the case above: 3 days are in 2008 (mon, tue, wed - 29th,30th,31st) and 4 are in 2009 (thu, fri, sat, sun - 1st,2nd,3rd,4th))
That does indeed seem to be a bug with how mktime() handles a "months" value greater than 12.
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 2, 1, 2008));
2008-01-31 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 3, 1, 2008));
2008-02-28 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 1, 1, 2008));
2007-12-31 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 13, 1, 2008));
2009-12-31 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 13, 1, 2007));
2008-12-31 23:59:59
Your best bet right now is probably to just check the output from date('m', ...)+1 yourself and special-case the calculation if the result is 13.
I did add some exceptions to reduce the year if the month was January, however i also had another date range which is built at the same stage which is used with GA api. I was surprised to see this date range was fine despite the method being similar. The difference being..
Problematic:
date('o-m-d G:i:s',mktime(0, 0, -1, date("m",1228089600)+1, 1, date("o",1228089600)));
Fine:
date('Y-m-d G:i:s',mktime(0, 0, -1, date("m",1228089600)+1, 1, date("Y",1228089600)));
So making the switch appears to have solved the problem.
Hopefully all this may be of use to someone, someday.
Try this:
$start = 1228089600;
$number_of_days_in_month = date('t', $time);
$end = strtotime('+' . $number_of_days_in_month . ' days', $start) - 1;
// subtract one second to get 23:59:59 or don't to get 00:00:00.
// Also note that there can be a leap second.
// $end = $start + $number_of_days_in_month * 86400 - 1; would probably work as well.
echo date('o-m-d G:i:s', $end);