Get date from the three and a half months ago - php

I know I can do
$threemonthsago = date("Y-m-d", strtotime("-3 month"));
echo "$threemonthsago";
However I want to get 3.5 months ago not three months ago. So I tried to do
$threemonthsago = date("Y-m-d", strtotime("-3.5 month"));
echo "$threemonthsago";
However it does not seem to give me the correct date its giving me like September something which it should not be since its currently April.

The decimal throws off strtotime() as that is not a valid format it recognizes. The real issue you have is what exactly is half of a month? If you traverse February it gets really dicey.
This is somewhat easier to do using DateTime() and DateInterval() if you specify exactly what half of a month is:
$date = new DateTime();
$new_date = $date->sub(new DateInterval('P3M15D'));
echo $new_date->format('Y-m-d');
Demo

echo(strtotime("-105 days"));

Related

Find PHP microtime 1 day ago

How to find 1 day ago time in PHP microtime() format.
I am trying like this. But still showing the current time.
$date = date("Y-m-d H:i:s", strtotime('-1 day'));
$val = microtime($date);
http://us2.php.net/manual/en/function.microtime.php
microtime() returns the current Unix timestamp with microseconds
You can't use microtime() to get a time from the past, since the funtion only works with current date.
Wouldn't this work for you?
$date = strtotime('-1 day');
Edit: Code corrected according to u_mulder's comment.

Add Days to PHP Variable

I have 2 variables in PHP. One is getting today's date and adding 1 month to it. That works fine. The other is supposed to take that date and add 6 days to it. For some reason that part refuses to work. Am I simply formatting it wrong? I always get 01-06-1970 in my database.
Here is the variable that gets today's date and adds 1 month (works fine)
$renewdate = date('Y-m-d', strtotime('+1 month'));
Here is the variable that adds 6 days to $renewdate (does not work)
$latedate = date('Y-m-d', strtotime('+6 days',$renewdate));
Second argument of strtotime is Unix timestamp. Currently $renewdate is a string. So:
$latedate = date('Y-m-d', strtotime('+6 days', strtotime($renewdate)));
PHP 5.2.0 brought DateTime, why are you still sticking to old functions? OOP approach is better!
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', '2016-08-02 16:16:02');
$dtCreate->add(new DateInterval('P6D'));
This will add 6 days to your DateTime object, see DateInterval for details.
After you added an interval, you may format your object however you wish:
$dtCreate->format('Y-m-d H:i:s');
This will return 2016-08-08 16:16:02, as you can see, it's 6 days later.

How to calculate number of days since start of year with DateTime while accounting for leap years?

I'm trying to use a DateTime object to calculate the current date from the number of days since Jan 1st. Leap years are very important here. Apparently, this does not account for leap years, however.
Here's my code:
$date = DateTime::createFromFormat('z Y', '59 2016');
echo $date->format('n/j/Y')."\n";
die();
Turns out this is a reported PHP bug from 2012 that I JUST found while I was making this question:
https://bugs.php.net/bug.php?id=62476
That's annoying.
Here is a workaround:
$date = DateTime::createFromFormat('m/d/Y', '01/01/2016');
$date->add(date_interval_create_from_date_string('59 days'));
echo $date->format('m/d/Y')."\n";

Easiest way to get the unix time stamp for the date exactly 1 month back in php

I want to get the unix time stamp for that day exactly 30days back from current day.
What is the best method?
Can i use this to get the date 30 days back, is this the best method?
$day = date('Y-m-d', strtotime('-30 days'));
a google search brings me to mktime() function in php. But how do i combine both and get the unix time stamp for the day? What is the easiest and fastest method?
You just need to use the strtotime("-1 month"); function. That will return a UNIX timestamp.
$date = date_create();
date_sub($date, date_interval_create_from_date_string('1 m'));
echo date_format($date, 'U');
How to get previous month and year relative to today, using strtotime and date? watch this question. there is the discussion about the funny strtotime("-1 month"); bug.

Get Last Monday - Sunday's dates: Is there a better way?

I'm preparing a query for mySQL to grab record from the previous week, but I have to treat weeks as Monday - Sunday. I had originally done this:
WHERE YEARWEEK(contactDate) = YEARWEEK(DATE_SUB(CURDATE(),INTERVAL 7 DAY))
to discover that mySQL treats weeks as Sunday - Monday. So instead I'm parsing getting the begin & end dates in php like this:
$i = 0;
while(date('D',mktime(0,0,0,date('m'), date('d')-$i, date('y'))) != "Mon") {
$i++;
}
$start_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+7), date('y')));
$end_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+1), date('y')));
This works - it gets the current week's date for monday (walking backwards until a monday is hit) then calculates the previous week's dates based on that date.
My question is: Is there a better way to do this? Just seems sloppy, and I expect someone out there can give me a cleaner way to do it - or perhaps not because I need Monday - Sunday weeks.
Edit
Apparently, there is:
$start = date('Y-m-d',strtotime('last monday -7 days'));
$end = date('Y-m-d',strtotime('last monday -1 days'));
That's about a million times more readable. Thank you.
you can use strtotime for this kind of date issues
echo strtotime("last Monday");
(complementing on marvin and Stomped ) Also you can use it this way
echo date('Y-m-d',strtotime('-1 Monday')); //last Monday
echo date('Y-m-d',strtotime('-2 Monday')); //two Mondays ago
echo date('Y-m-d',strtotime('+1 Monday')); //next Monday
strtotime("previous week Monday")
Monday of the previous week.
Marvin's answer is really elegant, although if you really wanted to go for performance you could do it with a little arithmetic. You could derive a formula/method to convert an arbitrary date to "days since some starting point" (probably 01.01.0000) and then the rest of the operations would be easy with that. Getting the day of week from such a number is as simple as subtracting and getting the remainder of a division.
Actually, PHP had a Date class in its PEAR library which did exactly this.
I have to point out for all the readers here there is a big issue with marvin's answer
Here is the catch
The "last Monday" function will return date the "latest" Monday.It will be explained like this , if today is Monday, then it will return the date of "LAST WEEK" Monday, however if today is not Monday, it will return "THIS WEEK" Monday
The Question is request to return "Last Week" Monday. Therefore the result will be incorrect if today is not Monday.
I have solved the issue and wrapped in my Date Time Helper
It will be only one line after you "INCLUDE" the class
$lastWeekMonday = Model_DTHpr::getLastWeekMonday();
Check Here
https://github.com/normandqq/Date-Time-Helper

Categories