PHP date difference extremly hard to accomplish - php

I have to get back dates just subtracting days from the current date and time.
Let suppose current date is May 1, 2011
after subtracting 30 days
April 1, 2011
How to accomplish this in PHP? Please help

$unixtime = strtotime("May 1, 2011 -30 days");
$human_readable = date('F j, Y', $unixtime);

Elaborating on Emil's answer. You can one-line it like so:
$thirty_days_ago = date('F j, Y', strtotime('-30 days'));
If you leave out the "May 1, 2011" part in strtotime(), you'll get 30 days ago from whatever the current date is.
Also, you've got a number of options for formatting in the docs.

Related

Getting the three previous months in PHP is showing the same result [duplicate]

This question already has answers here:
how to get the previous 3 months in php
(4 answers)
Closed 2 years ago.
I need the three previous months from the current month.
I got this code from Stack Overflow here:
echo date('M Y', strtotime('-0 month'));
echo date('M Y', strtotime('-1 month'));
echo date('M Y', strtotime('-2 month'));
echo date('M Y', strtotime('-3 month'));
The result is supposed to be:
Mar 2020
Feb 2020
Jan 2020
Dec 2019
But, I'm getting:
Mar 2020
Mar 2020
Jan 2020
Dec 2019
What's the problem? Is it because of the leap year in February?
I am also new to this concept, but this is what I found and it seems to work.
When you use strtotime('-1 month'), you would get February 31. This day doesn't exist, so that's probably what causes the issue.
I found this format that would get the first day of the month, that should fix the problem. It might not be the cleanest way to do it, but this seems to work.
echo date('M Y', strtotime('first day of -0 month'));
echo date('M Y', strtotime('first day of -1 month'));
echo date('M Y', strtotime('first day of -2 month'));
echo date('M Y', strtotime('first day of -3 month'));
By writing "first day of" before the -* month, you will grab the first day instead.
Check out Relative Formats where you can find all the formats you can use.
"is it because of the leap year february?' - Yes it does have something to do with it.
Per the manual on strtotime():
from user contributed note:
Depending on the day of the month, you may get a different response. For a non-leap year, you'll get March if the current day of the month is the 29th, 30th or 31st. If it's a leap year, you'll get March on the 30th or 31st of the month. The same thing will happen on the 31st of any month when you pass in the name of any month with less than 31 days. This happens because the strtotime() function will fill in missing parts from the current day.
Well, I suggest you to use the DateTime library of PHP.
Like this:
$today = new DateTime();
$today->modify('last day of previous month');
echo $today->format('M') . '\n';
$today->modify('last day of previous month');
echo $today->format('M') . '\n';
$today->modify('last day of previous month');
echo $today->format('M') . '\n';
You will have these answers:
Feb
Jan
Dev
For more details, see DateTime::modify

Today's date format - Day (Shorthand) Date Number Month (Shorthand) - PHP

I have a feed which gives feed in the following format: "Fri 14 Oct"
I want to see if today's date matches the date from the feed. My problem is the format of today's date/
$today = date("d m");
This outputs 17 10.
What is the best way to format $today so that it outputs Day (shorthand) space date (number) Month (shorthand) ?
how about:
$today = date("D j M");
As explained in date() reference manual.
Anyway you should be aware of timezone issues unless you are 100% sure that your server is in the same timezone of the feed you are comparing.
I would follow a different approach though, you can parse the feed's date using DateTime::createFromFormat() which also understand timezones, and then compare it with today's date.
$today = date("D d M");
PHP Date Documentation
<?php
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
For more details, please visit http://www.w3schools.com/php/func_date_date.asp

PHP How To Get Values From Dates

Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());

Why does PHP date('m', strtotime('-1 months')) not work correctly for today? 07/31

I have a script that gets the current and last month in PHP like so:
$currentMonth = date('m');
//Expected:07
//Result:07
$lastMonth = date('m', strtotime('-1 months'));
//Expected:06
//Result:07
Today happens to be the 31 or end of the month of July. Is this result to be expected from PHP?
When using -31 days the result is as expected:
$lastMonth = date('m', strtotime('-31 days'));
//Expected:06
//Result:06
Here's a cleaner test case that doesn't expire:
<?php
$origin = mktime(18, 0, 0, 7, 31, 2015);
var_dump( date('r', $origin), date('r', strtotime('-1 months', $origin)) );
string(31) "Fri, 31 Jul 2015 18:00:00 +0200"
string(31) "Wed, 01 Jul 2015 18:00:00 +0200"
I'm pretty sure it's a documentation issue, because the manual clearly states this (emphasis mine):
Relative month values are calculated based on the length of months
that they pass through. An example would be "+2 month 2011-11-30",
which would produce "2012-01-30". This is due to November being 30
days in length, and December being 31 days in length, producing a
total of 61 days.
... and it's wrong.
PHP bug tracker has tons of dupes about this. They're all closed as not a bug. Here's a relevant comment from 2009 that explains it:
I agree that this is an annoying behaviour.
Also, the implementation is problematic. Basically if you use '+1
month' it takes the month number, adds 1 and parses result as a new
date.
If you use '+1 month' on the first of the month, it sets the date to
the next first of the month.
This behaviour gives the impression, that php considers the length of
a month, which is not true.
But if you use '+1 month' on the last day of a month, the result is
unexpected as 2009-05-31 becomes 2009-06-31 which is an invalid date
and then interpreted as 2009-07-01.
This should at least be mentioned in the documentation.
You can do this way
$d = new DateTime();
$currentMonth = $d->format('m');
//Expected:07
//Result:07
print $currentMonth;
$d->modify('first day of previous month');
print "<br/>";
$lastMonth = $d->format('m');
//Expected:06
//Result:06
print $lastMonth;
DEMO: http://codepad.viper-7.com/kokWi8
This is a issue with PHP's date-string parser. See here:
http://derickrethans.nl/obtaining-the-next-month-in-php.html
#Mr. Llama made a script showing what other dates this issue effects:http://codepad.viper-7.com/E4gP0W
The solution I went with:
//Date:07/31/15
$currentMonth = date('m');
//Result:07
$lastMonth = date('m', strtotime('first day of -1 months'));
//Result:06
-1 month is interpreted as "same day of month, last month". If this day does not exist, the date overflows into the next month. Actually the result is the same as strtotime("31.6.2015") - try it!
There is "s" in excess in month. It should be like this:
$lastMonth = date('m', strtotime('-1 month'));

php date addition skipping days

I have a date, and need to add 24 Months (and not 2 Year) to it. This is what I tried:
strtotime("+24 months", $mydate);
If my date is, 20th Dec 2013 then the computed date is coming as 10th Dec 2015, whereas my expected date was 20th Dec 2015.
I know, what is going behind the scene:
2 Year: 365 days x 2 = 730 days
24 Months: 24 x 30days = 720 days
This gives me the missing 10 days. But how to over come this issue.
In Java we have Calendar class, which takes care of such calculations. However, I din't find anything here.
Can this issue be resolved.? Or I need to handle it manually?
You should always use the DateTime() class for anything like that.
i.e
$date = new DateTime("UTC");
//get date in 24months time:
$date->add(new DateInterval("P24M"));
//output date:
echo $date->format("d/m/Y H:i:s");
By using the DateTime and DateInterval classes, you can be sure that it will account of leap years and other such irregularities in dates.
See more at: http://php.net/manual/en/class.datetime.php
I hope this helps.
$mydate = "2014-10-01";
echo date('d-m-Y',strtotime("+24 months", strtotime($mydate)));
DateTime should work perfectly here:
$date = new DateTime("20th Dec 2013");
echo $date->format("d-m-Y");
$date->add(new DateInterval('P24M'));
echo $date->format("d-m-Y");
Output:
20-12-2013
20-12-2015
Demo
Sidenote:
I couldn't reproduce your error with:
echo date("d-m-Y", strtotime("+24 months", $mydate = strtotime("2013-12-20")));
See:
http://3v4l.org/HURAt

Categories