How to find month no,name from week number using php - 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"));

Related

Subtracting months from current date returns unexpected result

I just tried to subtract 6 and 5 months respectively from current date 08/29/2015 # 11:19am (UTC) and got the same result
Here is the code sample:
date("M, Y", strtotime("-5 months")) // returns Mar, 2015
date("M, Y", strtotime("-6 months")) // returns Mar, 2015
Is it due to day light saving? I think No.
date("M, Y", strtotime("-6 months"))
Simply returns also Mar, 2015 because there was no 29. February this year. So it takes the next month which is March.
To solve this just do it always from the first day of the month, e.g.
echo date("M, Y", strtotime("-6 months", strtotime(date('Y-m-01'))));
//^^^^^^^^^^^^^^^^^^^^^^^^^ First day of month
This is the only working option i could find for you. How to subtract 4 months from today's date?
echo date("Ymd", mktime(0, 0, 0, date("m")-5, date("d"), date("Y")));;

Why is date() returning the wrong month?

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

Convert date to two days from a given date

I'm trying to convert a given date to a date that's two days ahead of the given date. My code is as follows:
$date = date('D, M n', strtotime('+2 days', 'Mon, Dec 31, 2012'));
That code sort of gets it correct. It echoes "Wed, Jan 1". It gets the name of the day and the month correct. But, not the date. I have also tried another route.
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M n');
That didn't work either. Any ideas?
Thanks,
Lance
n is the format flag for month month. It's saying 1 because it's in January. Use j instead:
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M j'); //Wed, Jan 2
$newdate = date("D, M n",strtotime($oldDate. ' + 2 day'));

Set time in php to 00:00:00

I need to display the time but it must start from 00:00:00? I've got the following but it uses the current time.
print(date("H:i:s"));
As an alternative to mktime(), try the newer DateTime class, eg
$dt = new DateTime;
$dt->setTime(0, 0);
echo $dt->format('H:i:s');
// Add one hour
$dt->add(new DateInterval('PT1H'));
echo $dt->format('H:i:s');
Update
The flexibility of DateInterval makes this a very good candidate for a timer, eg
// add 2 years, 1 day and 9 seconds
$dt->add(new DateInterval('P2Y1DT9S'));
Use mktime() if you want to start with midnight for the current date:
<?php
echo date('l jS \of F Y h:i:s A',mktime(0,0,0));
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
http://codepad.org/s2NrVfRq
In mktime(), you pass arguments for hours, minutes, seconds, month, day, year, so set hours, minutes, seconds to 0 to get today at midnight. (Note, as Phil points out, mktime()'s arguments are optional and you can leave month, day, year out and it will default to the current date).
The mktime() function returns a unix timestamp representing the number of seconds since the unix epoch (January 1, 1970). You can count up from it in seconds or multiples of seconds.
<?php
// $midnight = mktime(0,0,0,date('m'),date('d'),date('Y'));
// The above is equivalent to below
$midnight = mktime(0,0,0);
echo date('l jS \of F Y h:i:s A',$midnight)."\n";
echo date('l jS \of F Y h:i:s A',$midnight+60)."\n"; // One minute
echo date('l jS \of F Y h:i:s A',$midnight+(60*60))."\n"; // One hour
?>
OUTPUTS
Friday 14th of October 2011 12:00:00 AM
Friday 14th of October 2011 12:01:00 AM
Friday 14th of October 2011 01:00:00 AM
http://codepad.org/FTr98z1n
date() uses the current time when you don't pass in an explicit timestamp. See the optional argument in the date documentation.
If you want to explicitly format midnight, use:
date("H:i:s", mktime(0, 0, 0));
try to use this syntax:
print(date("H:i:s", 0));
or
print(date("H:i:s", 10)); // 10 seconds

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

Categories