Why is date() returning the wrong month? - php

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

Related

How Do I Get The First Day Of The Months Number In PHP?

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

Simplest way to display current month and year like "Aug 2016" in PHP?

What is the shortest, simplest code to generate the curent month in Full English like September or in abbreviated three letter version like Feb and then add the current Year 2011?
So the code will, depending on the month and year, echo things like:
August 2016 or Aug 2016 etcettera. Thanks!
Full version:
<? echo date('F Y'); ?>
Short version:
<? echo date('M Y'); ?>
Here is a good reference for the different date options.
update
To show the previous month we would have to introduce the mktime() function and make use of the optional timestamp parameter for the date() function. Like this:
echo date('F Y', mktime(0, 0, 0, date('m')-1, 1, date('Y')));
This will also work (it's typically used to get the last day of the previous month):
echo date('F Y', mktime(0, 0, 0, date('m'), 0, date('Y')));
Here is a simple and more update format of getting the data:
$now = new \DateTime('now');
$month = $now->format('m');
$year = $now->format('Y');

How to find month no,name from week number using php

How to find month no,name from week number using php
If you have the ISO week number, then to get the month (of the start of the week) you can use strtotime like:
// F = full name of month, n = month number without leading zero
echo date('F n', strtotime('2010-W50'));
Bear in mind that the ISO week might not be the same as your meaning of week, so read on.
If you want to count the whole weeks since January 1st of this year (regardless of what day of the week that is) then you could do as Adnan mentioned:
echo date('F n', strtotime('1 Jan + 50 weeks'));
echo date('F',strtotime('1 January 2010 +50 weeks'));
www.php.net/date
www.php.net/strtotime
Have a look at php date() - http://php.net/manual/en/function.date.php
Here are some good examples:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
$myDate = "2010-05-12";
$weekNumber = date("W", strtotime($myDate));
Just replace the "W" with the value you need. Full reference:
http://php.net/manual/en/function.date.php
If you have a week number, and want the date from it you can use:
date("d m Y", strtotime("1.1.2010 + 30 weeks"));

PHP: simplest way to get the date of the month 6 months prior on the first?

So if today was April 12, 2010
it should return October 1, 2009
Some possible solutions I've googled seem overly complex, any suggestions?
Hm, maybe something like this;
echo date("F 1, Y", strtotime("-6 months"));
EDIT;
if you would like to specify a custom date use;
echo date("F, 1 Y", strtotime("-6 months", strtotime("Feb 2, 2010")));
A bit hackish but works:
<?php
$date = new DateTime("-6 months");
$date->modify("-" . ($date->format('j')-1) . " days");
echo $date->format('j, F Y');
?>
use a combination of mktime and date:
$date_half_a_year_ago = mktime(0, 0, 0, date('n')-6, 1, date('y'))
to make the new date relative to a given date and not today, call date with a second parameter
$given_timestamp = getSomeDate();
$date_half_a_year_ago = mktime(0, 0, 0, date('n', $given_timestamp)-6, 1, date('y', $given_timestamp))
to output it formatted, simply use date again:
echo date('F j, Y', $date_half_a_year_ago);
It was discussed in comments but the accepted answer has some unneeded strtotime() calls. Can be simplified to:
date("F 1, Y", strtotime("Feb 2, 2010 - 6 months"));
Also, you can use DateTime() like this which I think is equally as readable:
(new DateTime('Feb 2, 2010'))->modify('-6 months')->format('M 1, Y');
Or using static method....
DateTime::createFromFormat('M j, Y','Feb 2, 2010')
->modify('-6 months')
->format('M 1, Y');

How to show yesterdays date in this format?

I know there is a lot of info on the neton how to show yesterdays date but I am unable to get any of them to work in this format
How do you show yesterdays date if todays date is in this format date('F jS, Y') ?
July 27th, 2009 should show July 26th, 2009
//Does not work
$yesterday = date('F jS, Y', mktime(0, 0, 0, date("F") , date("j") - 1, date("Y")));
echo $yesterday;
Use the very awesome strtotime:
$today = 'July 27th, 2009';
$yesterday = date('F jS, Y', strtotime('yesterday', strtotime($today)));
print $yesterday; // July 26th, 2009
easier:
$yesterday = date('F jS, Y', time()-86400);

Categories