Convert date to two days from a given date - php

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

Related

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

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

Get the number of days remaining until date in string

I have a string $newAfter that outputs like Wednesday 9th of March 2016 11:59:59 PM.
I use this code to calculate the days left:
$daysleft = floor(($newAfter - time()) / 86400);
this is the result I get:
-16559
I want the days remaining up leading up to the date in string.
Of course you can't subtract days from that format string, you'll need to subtract them as integers of use DateTime objects:
$notAfter = 'Thu, 28 Apr 2016 03:22:56 +0200';
$today = new DateTime;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $notAfter);
$diff = $date->diff($today);
echo "{$diff->days} days left.";
Demo

What happening to php date function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date() and strtotime() return wrong months on 31st
I have this code and it outputs something strange i think. So, what i am doing wrong here.
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime('+01 month'));
$sP3 = date('m Y', strtotime('+02 month'));
$sP4 = date('m Y', strtotime('+03 month'));
echo $sP1.'<br>';
echo $sP2.'<br>';
echo $sP3.'<br>';
echo $sP4.'<br>';
?>
and this outputs
05 2012
07 2012
07 2012
08 2012
i think the second one should be
06 2012
Anybody know any solution?
Today is the 31st next month only has 30 days so it would be 7/12 in 1 month from today
assuming that today is May 31 2012
date('m Y') == 05 2012
date('m Y', strtotime('+1 month')) == 07 2012 because june has 30 days
date('m Y', strtotime('+2 month')) == 07 2012
date('m Y', strtotime('+3 month')) == 08 2012
date('m Y', strtotime('+4 month')) == 10 2012
I would take today's date and find the first day of the month then add a month to that if you are doing something that needs to get each month
As others have said, it is because today is the 31st and +1 month equals June-31 which changes to Jul-1. If you include the day in the date string, you can see exactly this.
<?php
$sP1 = date('m-d-Y');
$sP2 = date('m-d-Y', strtotime('+01 month'));
$sP3 = date('m-d-Y', strtotime('+02 month'));
$sP4 = date('m-d-Y', strtotime('+03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05-31-2012
07-01-2012
07-31-2012
08-31-2012
*/
?>
strtotime though can take the start date as part of the string so as King suggested, calculate the +N months from the first. So a string like May-1-2012 +01 month such as:
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime(date('M-1-Y').' +01 month'));
$sP3 = date('m Y', strtotime(date('M-1-Y').' +02 month'));
$sP4 = date('m Y', strtotime(date('M-1-Y').' +03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05 2012
06 2012
07 2012
08 2012
*/
?>
http://codepad.org/auYLHvDI
It is working as intended. In a nutshell, it is because what is "one month" from May 31? June 30? August 1?
My suggestion is that if you need sequential months, calculate the offset from the start of the current month, not the current day. Or compose the date that you're looking for manually using the month, day, and year parts broken up.

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

Categories