I need help with my brand new experience with PHP coding!
I have two time strings with the following format:
hh:mm:ss yyyy-mm-dd
find two examples down below:
10:35:56 2013-11-15
11:46:24 2013-11-16
I need to get the difference between the second and the first one (using some PHP function or line of code) in some way that could be reused to compute the same difference of other rows in my DB on demand...
$start=strtotime("10:35:56 2013-11-15");
$end=strtotime("11:46:24 2013-11-16");
$difference=$end-$start;
echo $difference. " Seconds";
use strtotime()
$date1 = strtotime('10:35:56 2013-11-15');
$date2 = strtotime('11:46:24 2013-11-16');
echo $date2 - $date1 . 'seconds';
You can do it this way with strtotime :
$date1 = strtotime("10:35:56 2013-11-15");
$date2 = strtotime("11:46:24 2013-11-16");
$date_diff = $date2 - $date1; // here you got difference in seconds.
echo $date_diff . " seconds";
Or with DateTime object :
$datetime1 = date_create('10:35:56 2013-11-15');
$datetime2 = date_create('11:46:24 2013-11-16');
$date_diff = date_diff($datetime1, $datetime2);
echo $date_diff ->format('%s seconds');
The $date_diff -> format(...) uses this list of format.
For exact difference try this
$datetime1 = new DateTime('10:35:56 2013-11-15');
$datetime2 = new DateTime('11:46:24 2013-11-16');
$interval = $datetime1->diff($datetime2);
echo $interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";
Related
I need help to subtract 2 times in PHP.
Example:
$date1 = 02:40;
$date2 = 00:00;
$finaldate = $date1 - $date2;
the correct answer will be 21:20.
Check it out:
if use $date2 as 24:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('24:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); // 21:20
But if use $date2 as 00:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('00:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); //02:40
use strtotime() and subtract both the times ...
<?php
$date1=strtotime("02:40");
$date2=strtotime("00:00");
$diff= date('H:i', $date2-$date1);
echo "Time Difference : ".$diff;
?>
This will output:
Time Difference : 21:20
I am struggeling with some basic date-formatting and echo the right number for "Days Left". This is the code:
date_default_timezone_set('Europe/Oslo');
$dateNow = date('d/m/Y', time());
$dateStart = date('d/m/Y', strtotime('01/08/2014'));
$dateFinished = date('d-m-Y', $dateStart + strtotime("+62 days"));
$daysLeft = intval($dateFinished-$dateNow);
echo($dateNow. "<br>");
echo($dateStart. "<br>");
echo($daysLeft. "<br>");
This is the echo:
07/07/2014
01/08/2014
-6
It seems like dateStart is interped as a wrong format. I have tried with both - and /, but nothing better.
What have i done wrong? Just the math or is it the logic?
I recommend to use OOP (Object-oriented) in this case. It is easier to write and easier to understand.
Just made a simple and short script:
<?php
date_default_timezone_set('Europe/Oslo');
$dateNow = new DateTime('now');
$dateStart = new DateTime('2014-08-01');
$dateFinished = new DateTime($dateStart->format('Y-m-d'));
$dateFinished->add(new DateInterval('P62D'));
$daysLeft = $dateFinished->diff($dateNow);
echo($dateNow->format('d/m/Y'). "<br>");
echo($dateStart->format('d/m/Y'). "<br>");
echo($daysLeft->format('%d'). "<br>");
?>
$dateStart is a string. You need to do:
$dateStart = date('d/m/Y', strtotime('01/08/2014'));
$dateFinished = date('d-m-Y', strtotime("+62 days", strtotime('01/08/2014')));
For more sophisticated date calculations consider DateTime().
If your PHP is 5.3+, than you can use DateTime class and calculate difference between dates:
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
OR
echo "difference " . $interval->days . " days ";
I have a database column with datatype "time" which stores 11:30:45. I have fetched this time in a variable say
$databasetime = 11:30:45
I want to declare a variable say $currenttime which will contain time just now. Like its 11:33:30 right now and another variable which will contain their difference in seconds like
$timediff = $currenttime - $databasetime;
echo $timediff;
I am trying $currenttime = time(); but I am not getting the result which I desire. I want $timediff = 165 but when I echo time(), I am getting a very big value.
$databasetime = strtotime('11:30:45');
$curtime = time();
echo $curtime - $databasetime;
You can do it in the following way:
$databasetime = '11:30:45';
$time1 = strtotime($databasetime);
$time2 = strtotime('now');
$diff = $time2 - $time1;
echo 'your difference: '.date('H:i:s', $diff);
$datetime1 = new DateTime('10:35:56 2013-11-17');
$datetime2 = new DateTime('10:35:50 2013-11-17');
$interval = $datetime1->diff($datetime2);
echo $interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";
<?php
$currentTime = time();
$futureDateTime = new DateTime('11:30:45'); // might want to specify a date and timezone, system TZ by default
$futureTime = $futureDateTime->format('U'); // get unix timestamp
$timeDiff = $futureTime-$currentTime;
?>
You use the DateTime 'OO' methods to return 'unix timestamp' integers directly
<?php
$now = new DateTime('now');
$date = new DateTime('11:30:45');
echo $now->getTimestamp() - $date->getTimestamp();
?>
Here is a simple example.
$databasetime = '11:30:45';
$timedif = abs(strtotime('now') - strtotime($databasetime));
echo $timedif; //echos the difference in seconds.
abs is used just to prevent neg numbers, which you may or may not want to do.
My problem is solved now. Actually all answers are right but the problem was due to the default time zone. I wanted Asia/Kolkata time zone but default European timezone apache was picking. That's why I was not getting my desired results.
So, I am finally using below code:
date_default_timezone_set('Asia/Kolkata');
$databasetime = strtotime('11:30:45');
$curtime = time();
echo $curtime - $databasetime;
I have a problem when I use difference of the datetime.
Here is the php code
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->days;
The correct result should be 2. But unfortunately it results 6015. Even when I change the date, its still 6015. Did you guys encounter this problem? I tried to run the script from other computer but its working.
yes sure man for that you need to assign timezone
try this code
i set it for india
$MNTTZ = new DateTimeZone('Asia/Kolkata');
$datetime1 = new DateTime('2009-10-11',$MNTTZ);
$datetime2 = new DateTime('2009-10-13',$MNTTZ);
$interval = $datetime1->diff($datetime2);
echo $interval->days;
Try this,
$start_date = new DateTime("2009-10-11");
$end_date = new DateTime("2009-10-13");
$interval = $start_date->diff($end_date);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
you use $interval->days replace with $interval->d." days "
you can check my answer https://stackoverflow.com/a/14938421/718224 on date difference for more information.
may this help you.
make sure to set format()
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
see here...
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);