How can you count up from a date, May 7, 2016 and just display the number of weeks and days (5 Weeks 1 Day) from that date with the least amount of code?
Use like this
<?php
$date = "May 07, 2011";
$date = strtotime($date);
$week=5;
$day=1;
$days=(5*7)+1;
$date = strtotime("+".$days." day", $date);
echo date('M d, Y', $date);
?>
Use the PHP DateTime class.
$lastDate="2016-10-10";
$date1 = new DateTime("2016-05-07");
$date2 = new DateTime($lastDate);
$difference = $date1->diff($date2);
echo "difference " .floor($difference->days/7)." weeks, ".($interval->days%7)." days ";
Get the number of weeks by dividing and modulus by 7 as above
Related
I'm trying make a function to return the exact date of previous months.
That is a example of my code:
// Dates in TimeStamp
$ts_now = strtotime('now');
$ts_month1 = strtotime("-1 month", $ts_now);
$ts_month2 = strtotime("-2 month", $ts_now);
$ts_month3 = strtotime("-3 month", $ts_now);
// Dates Formated
$date_now = date('Y-m-d', $ts_now);
$date_month1 = date('Y-m-d', $ts_month1);
$date_month2 = date('Y-m-d', $ts_month2);
$date_month3 = date('Y-m-d', $ts_month3);
//Output
echo $date_now; //2020-04-30
echo $date_month1; //2020-03-30
echo $date_month2; //2020-03-01
echo $date_month3; //2020-01-30
The problem is in $date_month2 that represents February, the output is 2020-03-01 instead 2020-02-29 and I suppose that problem will happen in months who have 30 days when present date have 31 days.
What is the best way to resolve that?
As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead.
function getMonthsAgo(int $n): string {
$date = new DateTime();
$day = $date->format('j');
$date->modify('first day of this month')->modify('-' . $n . ' months');
if ($day > $date->format('t')) {
$day = $date->format('t');
}
$date->setDate($date->format('Y'), $date->format('m'), $day);
return $date->format('Y-m-d');
}
// Dates Formated
$date_now = date('Y-m-d');
$date_month1 = getMonthsAgo(1);
$date_month2 = getMonthsAgo(2);
$date_month3 = getMonthsAgo(3);
//Output
echo $date_now;
echo $date_month1;
echo $date_month2;
echo $date_month3;
Output:
2020-04-30
2020-03-30
2020-02-29
2020-01-30
I am currently struggling with the following: I need to get the Date like "Sun, 29.05.2016 18:00" but all i have is the number of the week from the beginning of the year, the number of the day of week and the hour of the day.
Week: 21
Day: 7
Hour: 18
Now my question is how do i get the actual date and time with php's date functions or own code?
You are looking for DateTime::setISODate
$week_no = 21;
$day =7;
$hour = 18;
$d= new DateTime();
$d->setISODate(date('Y'),$week_no, $day);
$d->setTime ($hour,0);
echo $d->format('D, d.m.Y H:i');
Pretty straightforward with DateTime objects:
$week = 21;
$day = 7;
$hour = 18;
$dateFormat = sprintf('%d-W%02d-%d', (new DateTime())->format('Y'), $week, $day);
$x = new DateTime($dateFormat);
$x->setTime($hour, 0);
echo $x->format('Y-m-d H:i:s');
(assuming the current year)
Try with setISODate and DateTime. Online Check
Setting the 2016 from your desire output and use the hour directly to the format.
$gendate = new DateTime();
$gendate->setISODate(2016, 21, 7); //year , week num , day
echo $gendate->format('D, d.m.Y 18:00'); //Sun, 29.05.2016 18:00
i have a problem to get month difference between two dates in months.
$d1 = date_create('January 1, 2013');
$date = date("F j, Y");
$d2 = date_create($date);
$dif = date_diff($d1, $d2);
//echo $dif->format('%y years');
echo $dif->format('%m months');
It shows months but not the whole difference in months. I just want the diff in months between dates.
%m only shows up to 11 months. After that years are populated. If you want total months you need to figure in years and do some math:
$d1 = date_create('January 1, 2013');
$d2 = date_create();
$dif = date_diff($d1, $d2);
echo ($dif->format('%m') + $dif->format('%y') * 12) . ' months';
Demo
FYI, the above solution removes unnecessary code. If you want today's date you just don't pass any parameters to date_create().
So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php
In my PHP code I have a date in my variable "$postedDate".
Now I want to get the date after 7 days, 15 days, one month and 2 months have elapsed.
Which date function should I use?
Output date format should be in US format.
Use strtotime.
$newDate = strtotime('+15 days',$date)
$newDate will now be 15 days after $date. $date is unix time.
http://uk.php.net/strtotime
try this
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
Since PHP 5.2.0 the DateTime build in class is available
$date = new DateTime($postedDate);
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://php.net/manual/en/class.datetime.php
$date=strtotime(date('Y-m-d')); // if today :2013-05-23
$newDate = date('Y-m-d',strtotime('+15 days',$date));
echo $newDate; //after15 days :2013-06-07
$newDate = date('Y-m-d',strtotime('+1 month',$date));
echo $newDate; // after 1 month :2013-06-23
This is very simple; try this:
$date = "2013-06-12"; // date you want to upgade
echo $date = date("Y-m-d", strtotime($date ." +1 day") );
What’s the input format anyway?
1) If your date is, say, array of year, month and day, then you can mktime (0, 0, 0, $month, $day + 15, $year) or mktime (0, 0, 0, $month + 1, $day, $year). Note that mktime is a smart function, that will handle out-of-bounds values properly, so mktime (0, 0, 0, 13, 33, 2008) (which is month 13, day 33 of 2008) will return timestamp for February, 2, 2009.
2) If your date is a timestamp, then you just add, like, 15*SECONDS_IN_A_DAY, and then output that with date (/* any format */, $postedDate). If you need to add one month 30 days won’t of course always work right, so you can first convert timestamp to month, day and year (with date () function) and then use (1).
3) If your date is a string, you first parse it, for example, with strtotime (), then do whatevee you like.