Add variable number of days to date - php

How do I add a variable number of days to a date in PHP? All I've found is examples calculating with a specific number of days, not a variable.
$date = $row["date"]; // i.e 2015-12-13
$days = $row["days"]; // i.e 10
I want the output (in this example 2015-12-23) to be put in variable $futuredate.
Thanks!

You can try to do this with strtotime() and date() functions
$date = $row['date'];
$days = $row['days'];
$futuredate = date('Y-m-d',strtotime($date) + (24*60*60*$days));

Related

Calculating date difference

I'm calculating the age of a user by subtracting his age from today's date. The problem I have is that it show their age as whole number. But rather than showing it as a whole number, I'd like to display it as decimal like 25.5 or 27.3. I tried using round and number_format but neither worked.
$bday = $data->_field_data['node_field_data_field_game_players_nid']['entity']->field_player_birthday['und'][0]['value'];
$now = date("Y/m/d");
$diff = ($now) - date($bday);
echo number_format(round($diff, 2), 2);
you can try this:
$bday = $data->_field_data['node_field_data_field_game_players_nid']['entity']->field_player_birthday['und'][0]['value'];
echo number_format(round((strtotime(date('Y-m-d')) - strtotime($bday))/(60*60*24*365),2),2);
However, above calculation is very rough. if you want get value more precisely you should use date_diff for that:
$bday = $data->_field_data['node_field_data_field_game_players_nid']['entity']->field_player_birthday['und'][0]['value'];
$date1=date_create($bday); //create date object
$date2=date_create(date("Y-m-d")); //create date object
$curyearmaxdays=date("z", mktime(23,59,59,12,31,date("y")))+1; //count of days in the current year
$diff = date_diff($date1, $date2); //difference between two date objects
$realdiff=$diff->y + $diff->m/12+$diff->d/$curyearmaxdays;
echo number_format(round($realdiff,2),2);

PHP Change date variable after x days

I want to change date var when x days have passed
For instance:
Today is 21.12.16 - $date = '23.12.16'
Tomorrow is 22.12.16 - $date = '23.12.16'
When it's 23.12.16 - $date = '25.12.16'
Her's the code I got so far. Hope this will make some sense
$date = "2016-12-21"; //** will describe this lower
$days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
$new_date = date('d.m.y', strtotime("+2 days"));
} else{
$new_date = $date;
}
This works ok if I just want to do it once
**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php
P.S. sorry for my bad English.
Here's what I came up with:
$date = '2016-12-01'; //Your script start date, you wont need to change this anymore
$everyxdate = 10; // once x days to add x to $date
$days_passed = date_create()->diff(date_create($date))->days; // passed days from start of script $date
$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate; // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want
Hope this will help some one who has the same task I had.

How to get difference between two day in php

I have on function which passing some parameter the like
everyWeekOn("Mon",11,19,00)
I want to compute the difference between the current day (e.g. 'Fri')
and passed parameter day i.e. Mon.
The output should be:
The difference between Mon and Fri is 3
I tried it like this
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But know I don't know what to do next to get the difference between the two days.
Note that this question is different from How to calculate the difference between two dates using PHP? because I only have a day and not a complete date.
Just implement DateTime class in conjunction with ->diff method:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
You can find no. of days in two days by using this code
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
Please use this may this help you

Get time from a date/time string

I have a date value stored in a variable. I need to extract the time part of the value in to a separate variable and then add/subtract time from it.
The date variable is set with date('YmdHis'), giving (for example) 20110805124000 for August 5th 2011, 12:40:00
From the value 20110805124000 (which is stored in the variable $fulltime), I need to store the time only in the format 12:40 (so ignoring the year, month, day and seconds and adding the colon between the hour and minute) in a variable called $shorttime. I then need to add a number of hours to that time (so for example +3 hours would change the value in the $shorttime variable to 15:40). The number of hours I need to add is stored in a variable called $addtime, and this value could be a negative number.
Is this easily doable? Could anyone help?
Thanks :)
$time = '2013-01-22 10:45:45';
echo $time = date("H:i:s",strtotime($time));
It will give the time 10:45:45 from datetime.
<?PHP
$addhours = 3;
$date = DateTime::createFromFormat('YmdHis', '20110805124000');
$shorttime = $date->format("H:i");
$newdate = $date->add(DateInterval::createFromDateString($addhours . "hours"));
$newtime = $newdate->format("H:i");
echo $shorttime . "<br />";
echo $newtime . "<br />";
?>
for your reference:
http://www.php.net/manual/en/datetime.createfromformat.php
http://www.php.net/manual/en/dateinterval.createfromdatestring.php
hello the way I usually do it is like this, maybe it's a bit long but it works for me...
$DateIn = new DateTime($In);
$DateOut = new DateTime($Out);
$HourOut = new DateTime($H_Out);
$FechaEntrada = $DateIn->format('d-m-Y');
$FechaSalida = $DateOut->format('d-m-Y');
$HoraSalida = $HourOut->format('H:i a');
the guide I used was the PHP one DateTime::format()

How to convert date into same format?

I want to get the $registratiedag and count a couple of days extra, but I always get stuck on the fact that it needs to be a UNIX timestamp? I did some google-ing, but I really don't get it.
I hope someone can help me figure this out. This is what I got so far.
$registratiedag = $oUser['UserRegisterDate'];
$today = strtotime('$registratiedag + 6 days');
echo $today;
echo $registratiedag;
echo date('Y-m-d', $today);
There's obviously something wrong with the strtotime('$registratiedag + 6 days'); part, because I always get 1970-01-01
You probably want this:
// Store as a timestamp
$registratiedag = strtotime($oUser['UserRegisterDate']);
$new_date = strtotime('+6 days', $registratiedag);
// You'll need to format for printing $new_date
echo date('Y-m-d', $new_date);
// I think you want to compare $new_date against
// today's date. I'd recommend a string comparison here,
// As time() includes the time as well
// time() is implied as the second argument to date,
// But we'll put it anyways just to be clearer
if( date('Y-m-d', $new_date) == date('Y-m-d', time()) ) {
// The dates are equal, do something here
}
else if($new_date < time()) {
// if the new date is earlier than today
}
// etc.
First it converts $registratiedag to a timestamp, then it adds 6 days
EDIT: You probably should change $today to something less misleading like $modified_date or something
try:
$today = strtorime($registratiedag);
$today += 86400 * 6; // seconds in 1 day * 6 days
at least one of your problems is that PHP does not expand variables in single quotes.
$today = strtotime("$registratiedag + 6 days");
//use double quotes and not single quotes when embedding a php variable in a string
If you want to include the value of variable $registratiedag right into the text passed as parameter of strtotime, you have to enclose that parameter with ", not with '.

Categories