I'm trying to add some hours to a date dynamically.
I have a date, time and hours variable.
$appdate is in the format of 2013/03/23
$time is in military time format 13:00:00
$rei equals hours but it's in a plain number format from 0-48.
I'm trying to combine the $appdate and the $time together then add the $rei hours selected to it to end with $reiexpires in standard mysql format. 2013-03-23 20:00:00
$reitime = date('H',strtotime($rei));
$reidate = date('Y-m-d H:i:s',strtotime($appdate.$time));
$reiexpires = date('Y-m-d H:i:s',strtotime($reidate + $reitime));
$appdate = "2013/03/23";
$time = "13:00:00";
$rei = 48;
$reitime = strtotime($appdate . " " . $time . " + " . $rei . " hours");
$reiexpires = date('Y-m-d H:i:s', $reitime);
echo $reiexpires;
Output:
2013-03-25 13:00:00
Related
I have the below code to calculate the time difference but when i run the code it prints 0 difference. Please see into my code below.
I want to get the time difference between the time interval which has Time Transition from PM (Today) to AM (Next day).
//echo 'Hello World!';
//echo mktime('11','30','0','1','2019','1');
//echo mktime('12','30','0','1','2019','1');
//echo (" hello ");
//echo date(time());
//echo (" hello ");
//echo date('Y-m-d H:i:s A');
$date = "2019-10-16";
$stime = "11:30:00 PM";
$etime = "12:30:00 AM";
if("PM" == date('A', strtotime($stime)) && "AM" == date('A', strtotime($etime)))
{
//echo " Time transition ";
//get the start date and start time merging format
$sdatetime = date('Y-m-d H:i:s A', strtotime("$date $stime"));
//convert the start datetime into epoch
$startepoch = strtotime($sdatetime);
//increment the date to get the next day date
$edate = date('Y-m-d', strtotime($date. ' + 1 day'));
//get the end date and end time merged
$edatetime = date('Y-m-d H:i:s A', strtotime("$edate $etime"));
//convert the end datetime into epoch
$endepoch = strtotime($edatetime);
$timediff = ($endepoch-$startepoch)/3600.0;
echo " Time Difference: ".$timediff;
}else{
echo "No time transition";
}
// 24 hour format
//$time = "00:00:00";
//echo date('A', strtotime($time));
PHP datetime and strtotime can parse times with AM and PM.
DateTime objects can also be compared directly.
If the end time is less than the start time, it is assumed to be the end time of the next day. This is better than selecting between AM and PM.
$date = "2019-10-16";
$stime = "11:30:00 PM";
$etime = "12:30:00 AM";
$dtStime = date_create($date." ".$stime);
$dtEtime = date_create($date." ".$etime);
if($dtEtime < $dtStime){
$dtEtime->modify('+1 Day');
}
$diff = $dtStime->diff($dtEtime);
$seconds = $diff->h *3600 + $diff->i * 60 + $diff->s;
echo "Diff: ".$seconds." Seconds";
returns
Diff: 3600 Seconds
I simply want a php function to convert date to excel number format.
Ex: 2013-11-01 to 41579
This is the way to do it in Excel
I found a way to convert a Unix timestamp to an Excel date.
$date_time = "2013-11-01 00:00:00";
$date_time_plus_one = strtotime($date_time . ' +1 day');
$str_date = strtotime(date('Y-m-d', $date_time_plus_one));
$excel_date = intval(25569 + $str_date / 86400);
echo 'php actual date time : ' . $date_time . '<br>';
echo 'add one day : ' . $date_time_plus_one . '<br>';
echo 'excel Number DATEVALUE : ' . $excel_date . '<br>';
seconds in a day: 86400 , 25569 days between 30 Dec 1899 and 01 Jan 1970. So This is the output.
php actual date time : 2013-11-01 00:00:00
add one day : 1383330600
excel Number DATEVALUE : 41579
You can change time into string like this. Every date is unique and you can also arrange them in order
$month = date("F");
$date = date("d");
$year = date("Y");
$timestamp = strtotime($month . " " . $date . " " . $year);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$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");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'
I have a date returned as part of a MySQL query in the form 2010-09-17.
I would like to set the variables $Date2 to $Date5 as follows:
$Date2 = $Date + 1
$Date3 = $Date + 2
etc., so that it returns 2010-09-18, 2010-09-19, etc.
I have tried
date('Y-m-d', strtotime($Date. ' + 1 day'))
but this gives me the date before $Date.
What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?
All you have to do is use days instead of day like this:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
And it outputs correctly:
2010-09-18
2010-09-19
If you're using PHP 5.3, you can use a DateTime object and its add method:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D', 3 would be 'P3D', and so on).
Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
From PHP 5.2 on you can use modify with a DateTime object:
http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Be careful when adding months... (and to a lesser extent, years)
Here is a small snippet to demonstrate the date modifications:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
You can also use the following format
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
You can stack changes this way:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.
Here has an easy way to solve this.
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
Output will be:
2015-11-22
Solution has found from here - How to Add Days to Date in PHP
Using a variable for Number of days
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.' days');
echo new Date('d/m/Y', $newDate); //format new date
Here is the simplest solution to your query
$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days
echo date_format($date,"Y-m-d"); //set date format of the result
This works. You can use it for days, months, seconds and reformat the date as you require
public function reformatDate($date, $difference_str, $return_format)
{
return date($return_format, strtotime($date. ' ' . $difference_str));
}
Examples
echo $this->reformatDate('2021-10-8', '+ 15 minutes', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 hour', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 day', 'Y-m-d H:i:s');
To add a certain number of days to a date, use the following function.
function add_days_to_date($date1,$number_of_days){
/*
//$date1 is a string representing a date such as '2021-04-17 14:34:05'
//$date1 =date('Y-m-d H:i:s');
// function date without a secrod argument returns the current datetime as a string in the specified format
*/
$str =' + '. $number_of_days. ' days';
$date2= date('Y-m-d H:i:s', strtotime($date1. $str));
return $date2; //$date2 is a string
}//[end function]
All have to use bellow code:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Another option is to convert your date string into a timestamp and then add the appropriate number of seconds to it.
$datetime_string = '2022-05-12 12:56:45';
$days_to_add = 1;
$new_timestamp = strtotime($datetime_string) + ($days_to_add * 60 * 60 * 24);
After which, you can use one of PHP's various date functions to turn the timestamp into a date object or format it into a human-readable string.
$new_datetime_string = date('Y-m-d H:i:s', $new_timestamp);
Basically what I want to do is to send an email to follow up on order placed in 3 hours later.
What I try to do is to get the current date (up to hours level), then compare with the orderdate+ 3 hours, to see whether it match.
Example orderdate: 2010-06-12 18, after add 3 hours should become 2010-06-12 21
However, after add hour, the final result sometimes become bigger, sometimes become smaller... something wrong.
$now= date('Y-m-d H');
echo $now . "<br>";
...
...
while ($data = mysql_fetch_array ($result))
{
$orderid = $data['orderid'];
$orddate = $data['orddate'];
$corddate= date('Y-m-d H:i:s', $orddate);
$year = substr ($corddate, 0, 4);
$month = substr ($corddate, 5, 2);
$day = substr ($corddate, 8, 2);
$hour = substr ($corddate, 11, 2);
$actualorderdate= $year . "-" . $month . "-" . $day . " " . $hour;
$new_time = mktime ($hour+3, 0, 0, $month, $day, $year);
$year = date ('Y', $new_time);
$month = date ('m', $new_time);
$day= date ('d', $new_time);
$hour= date ('h', $new_time);
$xhourslater= $year . "-" . $month . "-" . $day . " " . $hour;
echo "actualorderdate: ". $actualorderdate. " xhourslater: " . $xhourslater. "<br>";
if($now==$xhourslater)
{
echo "now is 3 hours later" . "<br>";
//send email follow up, then update status as sent, in order not to resend anytime in the 3rd hour.
}
}
Seems to me like you're doing a lot of excess parsing for nothing.
$orddate = $data['orddate'];
$actualOrderDate = date('Y-m-d H:i:s', $orddate);
$timeToSendEmail = date('Y-m-d H:i:s', strtotime('+3 hours', $orddate)); //Assuming $orddate is a timestamp int. If not, you'll have to do some calculations or something there, possibly using date() again.
Basically, you just want to have the order time + 3 hours, so use php's strtotime (http://php.net/manual/en/function.strtotime.php) function. Very helpful.
If that's not what you want, please post some test data so it's easier to figure out what might be going funky.
I believe it's simpler to
$date_now = date( "Y-m-d g:i a" ); // get the date
$time_now = time( $date_now ); // convert it to time
$time_next = $time_now + 3 * 60 * 60; // add 3 hours (3 x 60mins x 60sec)
$date_next = date( "Y-m-d H", $time_next); // convert it back to date