It's maybe a really simple question but I found a little hard since I am still novice on PHP.
Presumably I have:
$time1 = strtotime("today 15:00:00-05:00");
This is just example, $time1 is dynamic at runtime and can be any value, I would like to create new $time2 which has the value:
$time2 = strtotime("this thursday 15:00:00-05:00");
Please note in here $time1 and $time2 have the same 15:00:00-05:00 and only different the day. So, to sump up, I have two inputs:
$time1 which is dynamic at runtime.
the string this thursday.
How to create the $time2 with the value like above.
Find the difference between "today" and "today 15:00:00-05:00" and add it to "this thursday"
$sometime = "today 15:00:00-05:00";
$time1 = strtotime($sometime);
// now find only the hour part
$time = mktime(0,0,0, date('n', $time1), date('j', $time1), date('Y', $time1));
$time_difference = $time1 - $time;
$time2 = strtotime("this thursday") + $time_difference;
Or even simpler:
$sometime = "today 15:00:00-05:00";
$time1 = strtotime($sometime);
// now find only the hour part
$hour_string = date('h:i:s', $time1);
$time2 = strtotime("this thursday $hour_string");
Wonder why you don't use DateTime object of PHP right from the start, but ok:
$dateTime = new DateTime($time1);
$dateTime->modify('+1 day');
$time2 = $dateTime->getTimestamp();
Where you can change the 1 to any amount of days.
As I understand what you want is to concat the two string "this thursday" and "15:00:00-05:00":
$str1 = "this thursday";
$str2 = "15:00:00-05:00";
$time = strtotime($str1." ".$str2);
You may try this. And you need to specify more if it is not fulfilling the requirement.
$time1 = date("Y-m-d H:i:s");
$time2 = 'this thrusday ';
$time2 .= $time1;
$time2 = strtotime($time2);
Related
I want to keep the H:i:s format so 10:52:20 however when I try:
$timeleft = strtotime($time1)-strtotime($time2);
I get something like 4521
I would like the result to retain the format. Is there a way to do this?
Yeah, what you get is unix timestamp which is in seconds. You can use date function to get required format like this:
$timestr_formated = date("H:i:s", $timeleft);
print($timestr_formated);
You can use DateTime::diff.
$time1 = new DateTime('2017-05-01 12:00:00');
$time2 = new DateTime('2017-05-01 11:25:30');
$timeleft = $time1->diff($time2);
echo $timeleft->format('%H:%i:%s'); // output: 00:34:30
In case you want to allow more than 24 hours.
$time1 = new DateTime('2017-05-02 12:00:00');
$time2 = new DateTime('2017-04-02 11:25:30');
$timeleft = $time1->diff($time2);
echo str_pad($timeleft->format('%a') * 24 + $timeleft->format('%h'), 2, "0") .
$timeleft->format(':%i:%s'); // output: 720:34:30
You can use date function by below format:
$timeleft = date('H:i:s', strtotime($time1)-strtotime($time2));
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 trying to add hh:mm:ss with the date. How can i do it?
I tried with the following but it works when the hour is string, but when adding time is similar to MySQL Date time it is not working.
$new_time = date("Y-m-d H:i:s", strtotime('+5 hours'));
I am trying to get solution for the following:
$timeA= '2015-10-09 13:40:14';
$timeB = '03:05:01'; // '0000-00-00 03:05:01'
OutPut:
$timeA + $timeB = 2015-10-09 16:45:15 ?
How Can I Add this?
Use DateInterval():
$timeA = new DateTime('2015-10-09 13:40:14');
$timeB = new DateInterval('PT3H5M1S'); // '03:05:01';
$timeA->add($timeB);
echo $timeA->format('Y-m-d H:i:s');
You would need to break your time down into the right DateInterval format but that is easily done with explode();
Here's how that might look:
$parts = array_map(function($num) {
return (int) $num;
}, explode(':', '03:05:01'));
$timeA = new DateTime('2015-10-09 13:40:14');
$timeB = new DateInterval(sprintf('PT%uH%uM%uS', $parts[0], $parts[1], $parts[2]));
$timeA->add($timeB);
echo $timeA->format('Y-m-d H:i:s');
Demo
print date('Y-m-d H:i:s',strtotime($timeA." +03 hour +05 minutes +01 seconds"));
Should work also.
So:
$timeA= '2015-10-09 13:40:14';
$timeB = vsprintf(" +%d hours +%d minutes +%d seconds", explode(':', '03:05:01'));
print date('Y-m-d H:i:s',strtotime($timeA.$timeB));
Can be the solution.
You may also convert the time into seconds with this approach from: Convert time in HH:MM:SS format to seconds only?
$time = '03:05:01';
$seconds = strtotime("1970-01-01 $time UTC");
Then you could add the seconds to
$currentTime = '2015-10-10 13:40:14';
$newTime = date("Y-m-d H:i:s", strtotime( $currentTime.'+'.$seconds.' seconds'));
If you prefer to use the DateTime objects offered by #John Conde, here are two ways to convert the time string into the format:
$formattedTime = preg_replace("/(\d{2}):(\d{2}):(\d{2})/","PT$1H$2M$3S","03:05:11");
or, as you read it from the database:
select concat(hour(last_modified),'H',minute(last_modified),'M',second(last_modified),'H') from people;
So a more general code approach would be:
$initial = 'some time';
$interval = 'the interval value';
$initialTime = new DateTime($initial);
$intervalTime = new DateInterval($interval);
$initialTime->add($intervalTime);
echo $initialTime->format('Y-m-d H:i:s');
In my PHP application I want to calculate the sum of two time variables. I am looking for something like this example.
$time1 = 15:20:00;
$time2 = 00:30:00;
$time = $time1+$time2;
If the answer you expect is 15:50:00 and you want to use strtotime and date functions, you need to subtract the seconds $time1 and $time2 share when you transform them to unix timestamps:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1) + strtotime($time2) - strtotime('00:00:00');
$time = date('H:i:s', $time);
The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:
$o = strtotime($time1)+strtotime($time2);
If I remember right strtotime does support this format.
Otherwise you will need to filter it out yourself.
Following answer does not return correct value. It is summing integers but not returned correct time.
$o = strtotime($time1)+strtotime($time2);
I created a function to get calculated time as follows.
public function addTwoTimes($time1 = "00:00:00", $time2 = "00:00:00"){
$time2_arr = [];
$time1 = $time1;
$time2_arr = explode(":", $time2);
//Hour
if(isset($time2_arr[0]) && $time2_arr[0] != ""){
$time1 = $time1." +".$time2_arr[0]." hours";
$time1 = date("H:i:s", strtotime($time1));
}
//Minutes
if(isset($time2_arr[1]) && $time2_arr[1] != ""){
$time1 = $time1." +".$time2_arr[1]." minutes";
$time1 = date("H:i:s", strtotime($time1));
}
//Seconds
if(isset($time2_arr[2]) && $time2_arr[2] != ""){
$time1 = $time1." +".$time2_arr[2]." seconds";
$time1 = date("H:i:s", strtotime($time1));
}
return date("H:i:s", strtotime($time1));
}
You could use the PHP 5.3 DateInterval:
$timeInterval = DateInterval::createFromDateString( '15 hours + 20 minutes' );
$timeInterval2 = DateInterval::createFromDateString( '30 minutes' );
foreach( str_split( 'ymdhis' ) as $prop )
{
$timeInterval->$prop += $timeInterval2->$prop;
}
var_dump( $timeInterval->format( '%H:%i:%s' ) );
(How to add to DateInterval objects was explained here: How we can add two date intervals in PHP)
As far as I can tell, Sammaye's answer did't work out for me.
I needed to start the time I wanted to add with the start of the UNIX timestamp. This way, strtotime returns the seconds that need to be added to the first time.
$time1 = "15:20:00";
$time2 = "1970-01-01 00:30:00";
$time = strtotime($time1) + (strtotime($time2) + 3600);
echo $time . "<br />";
echo date("H:i:s", $time);
Be sure to consult the mystic docs http://us1.php.net/strtotime for additional things you can input into your functions :)
$today = time();
$tommorrow = strtotime("+1 days", $today);
$day_after_tomorrow = strtotime("+1 days", $tomorrow);
Code:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1)+strtotime($time2);
$sumtime = date("H:i:s",$time);
I have a variable called $effectiveDate containing the date 2012-03-26.
I am trying to add three months to this date and have been unsuccessful at it.
Here is what I have tried:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
and
$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");
What am I doing wrong? Neither piece of code worked.
Change it to this will give you the expected format:
$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.
$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;
I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version
You need to convert the date into a readable value. You may use strftime() or date().
Try this:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;
This should work. I like using strftime better as it can be used for localization you might want to try it.
Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime() as follows:
$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );
(This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)
The following should work,Please Try this:
$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
Following should work
$d = strtotime("+1 months",strtotime("2015-05-25"));
echo date("Y-m-d",$d); // This will print **2015-06-25**
Add nth Days, months and years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
As of PHP 5.3, DateTime along with DateInterval could be a feasible option to achieve the desired result.
$months = 6;
$currentDate = new DateTime();
$newDate = $currentDate->add(new DateInterval('P'.$months.'M'));
echo $newDate->format('Y-m-d');
If you want to subtract time from a date, instead of add, use sub.
Here are more examples on how to use DateInterval:
$interval = new DateInterval('P1Y2M3DT4H5M6S');
// This creates an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.
$interval = new DateInterval('P2W');
// This creates an interval of 2 weeks (which is equivalent to 14 days).
$interval = new DateInterval('PT1H30M');
// This creates an interval of 1 hour and 30 minutes (but no days or years, etc.).
The following should work, but you may need to change the format:
echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));
public function getCurrentDate(){
return date("Y-m-d H:i:s");
}
public function getNextDateAfterMonth($date1,$monthNumber){
return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1)));
}