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')))
Related
I know that I can use date("w"); to get a number 0-6 depending on what day it is, but how would I do this to see what day the first day in the current month is? For example, this month March 1st was a Wednesday, so it should return a 3 for Wednesday.
I tried using this date("w", "Y-m-01") but it just gives me the error
Warning: date() expects parameter 2 to be long, string given
This returns the day (eg. "Wednesday") that is first in the current month:
echo date('l',mktime(0, 0, 0, date('m'), 1));
..I suspect this was more useful to OP, hence the accept, but in fact it can be modified to meet the exact requirements of the question (number - eg "3") with:
echo date('N',mktime(0, 0, 0, date('m'), 1));
<?php
$month_start = strtotime('first day of this month', time());
//first day name , month, year
echo date('D, m Y', $month_start).'<br/>';
//first day number (Monday = 1, Tuesday = 2) etc , month, year
echo date('N, m Y', $month_start).'<br/>';
I have tried this:
$date = date("N", mktime(0, 0, 0, date('n'), 1, date('Y')));
echo $date;
you can do it with class DateTime
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2017-03-17'))
->modify('first day of this month')
->format('jS, F Y');
date('F Y', strtotime('2013-05-00T00:00:00')); returns the value 'April 2013', but I would expect it to return 'May 2013'.
Is this an issue with date() interpreting the date string as still in April, or perhaps the format string? I tried 'M Y', which still gave me 'Apr 2013'. Is there an alternative?
date works on a timestamp not a textual representation of a date
If you change the 0 to a 1 for day of month (thus getting a valid date to start from) and use strtotime
date('F Y', strtotime('2013-05-01T00:00:00'));
Then it will work
PHP interprets day of the month 0 as the last day of the previous month.
Valid values for day of the month are 01-31
echo date('d F Y', mktime(0, 0, 0, 5, 0, 2013)); //30 April 2013
echo date('d F Y', mktime(0, 0, 0, 5, 1, 2013)); // 01 May 2013
date('F Y', strtotime('2013-05-00T00:00:00'));
0 is not a valid start date. You have to assign it to 1. Then it will work as you expected.
date('F Y', strtotime('2013-05-01T00:00:00'));
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.
I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.
Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.
Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))
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