I have this datetime code below. I want to subtract 23 hours and 59 minutes in $begin variable:
$date = 2016-03-14 23:59:59;
$given = new DateTime($date, new DateTimeZone("Asia/Tokyo"));
$given->setTimezone(new DateTimeZone("UTC"));
$end = $given->format("Y-m-d H:i:s");
$begin = date('Y-m-d H:i:s',strtotime('-23 hours',strtotime($end)));
Now The output is:
$end is: 2016-03-14 14:59:59
$begin is: 2016-03-13 15:59:59
The output I want is:
$end is: 2016-03-14 14:59:59
$begin is: 2016-03-13 15:00:00
How can I subtract the minutes in seconds in begin? Or there is a best way to do it?
As you just want to change the hour, you can make this to get the desire result, but you also can do something else like Amit Roy did or ceejayoz suggest. This is a pretty simple solution, just do it: reset the min and sec to 00 so that your output shows like you want.
echo $begin = date('Y-m-d H:00:00',strtotime('-23 hours', strtotime($end)));
Use the DateInterval class with the DateTime::sub method.
Example:
$begin_datetime = $given->sub(new DateInterval('PT23H59M00S'));
$begin = $begin_datetime->format('Y-m-d H:i:s');
http://php.net/manual/en/datetime.sub.php
http://php.net/manual/en/dateinterval.construct.php
[As Frayne make the comment.] As you just want to change the hour, not the min or sec, you have to set the min and sec to 00:00 as your desire output.
echo $begin = date('Y-m-d H:00:00',strtotime('-23 hours', strtotime($end)));
You can did this thing in many different ways, and this is one way.
You have to do this to get desired result. You were subtracting hours alone
$begin = date('Y-m-d H:i:s',strtotime('-23 hours',strtotime($end)));
You have to subtract min and sec too
$begin = date('Y-m-d H:i:s',strtotime('-23 hours -59 minutes -59 seconds',strtotime($end)));
Finally you get this code:
$date = "2016-03-14 23:59:59";
$given = new DateTime($date, new DateTimeZone("Asia/Tokyo"));
$given->setTimezone(new DateTimeZone("UTC"));
$end = $given->format("Y-m-d H:i:s");
echo $begin = date('Y-m-d H:i:s',strtotime('-23 hours -59 minutes -59 seconds',strtotime($end))); //2016-03-13 15:00:00
Related
I need to calculate the datetime difference in minutes using PHP. I am explaining my code below .
$date='10-03-2018 03:44 PM';
$endTime = strtotime($date);
$currentDate=date("d-m-Y h:i A");//10-03-2018 03:53 PM
$currentTime = strtotime($currentDate);
echo (round(abs($currentTime - $endTime) / 60,2));//25344617
Here I need to calculate the difference in minutes but the differnce value is more where the expected time difference should be 9 but as per my code I am getting the wrong value.
Let the PHP DateTime class with diff() method do the work with time calculations.
$now = '10-03-2018 03:53 PM'; // or use simply 'now' for current time
$endTime = '10-03-2018 03:44 PM';
$datetime1 = new DateTime($now);
$datetime2 = new DateTime($endTime);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%i minutes'); // 9 minutes
See it live: https://eval.in/969615
What I'm trying to do is to return the rest of this simple equation as date.
$time = (time()+(60*60*12)) - time();
$date = date('d H:i:s', $time);
echo $date;
As you see i have added 12 hours, but this added 1 day + 2 hours. And i stuck here :/.
Output is 01 14:00:00
Expected is 00 12:00:00
So what i'm doing wrong?
EDIT
I have tried date_default_timezone_set() function but it seems to not work for me :/
NEW EDIT
I realize when i do this.
$time = time() - time();
$date = date('d H:i:s', $time);
echo $date;
Print out 01 02:00:00 what is that?
You can do this using DateTime-
$start = new DateTime();
$end = new DateTime();
$end->modify('+12 hour');
$interval = $end->diff($start);
$elapsed = $interval->format('%a days %h hours %i minutes %s seconds');
echo $elapsed;
Not the answer to your question, but take a look at Carbon, "A simple PHP API extension for DateTime.". Very useful for these kind of DateTime-problems.
I want to have have 2 variables with the current time in hours:minutes and also one that has +15 minutes. But first I must also add +6 hours.
So for example, right now it is 2018-01-07 16:35:10. So first I add +6 hours. So it will be 2018-01-07 22:35:10. Next, I want to extract only the hours:minutes.
I want to get only "22:35" to variable.
And next variable, I want 22:35 +15 minutes, so 22:50.
So I have $dateNow = 22:35 and $dateThen = 22:50
I have tried this so far to get current time now and +6 hours, but it's not working. Error: Call to a member function format() on integer
$now = strtotime(date("Y-m-d H:i:s")." +6 hours");
$then = $now->format('H:i');
echo $then;
i think in this case it would be very use full to use the DateTime class from PHP. The Problem with your code is strtotime returns a int not an DateTime object.
I've modified your code so it will work:
$org = new DateTime("2018-01-07 16:35:10");
$then = $org->add(new DateInterval("PT6H"));
echo $then->format("H:i"),"<br>";
$afterThen = $then->add(new DateInterval("PT15M"));
echo $afterThen->format("H:i");
Short solution with DateTime and DateInterval objects:
$now = new DateTime();
$result = $now->add(new DateInterval('PT6H15M'))->format('H:i');
Try this:
$time = time() + 3600 * 6; // add 6 hours
$date = date("H:i", $time); // format date
$date_plus_15 = date("H:i", $time + 60 * 15); // format date and add 15 minutes
echo "Time: {$date} <br>";
echo "Time + 15 mins: {$date_plus_15}";
Example here: https://ideone.com/ZR6cfy
Hi I have a date string taken from database. And it's like
$date = '2016-03-21 11:00:00';
And I have a time string taken from database.
$time = '02:30:00'
Is there a way to add these two and get like these in PHP?
$newtime = '2016-03-21 13:30:00';
Use preg_replace to transform your $time value:
$date = '2016-03-21 11:00:00';
$time = '02:30:00';
$str = "$date + ".preg_replace( '/:(\d+):(\d+)$/',' hours, \1 minutes, \2 seconds',$time );
$date = new DateTime( $str );
echo $date->format('Y-m-d H:i:s');
Will print:
2016-03-21 13:30:00
With preg_replace we transform 02:30:00 in 02 hours, 30 minutes, 00 seconds and, postponing it to $date, we obtain this string:
2016-03-21 11:00:00 + 02 hours, 30 minutes, 00 seconds
directly accepted by DateTime class.
Read more about DateTime
I'm sure there's a better way to do this but:
$date = new DateTime($time);
$int = new DateInterval($date->format("\P\TH\Hi\Ms\S"));
$dateTime = new DateTime($date);
return $dateTime->add($int);
I want to calculate the time difference from now (lets say 18:30:00) till this evening at 20pm.
$today = date('Y-m-d', time());
$remain = strtotime($today. " 00:00:00 + 20 hours") - time();
$remain = date('H:i:s', $remain);
I get a result which is one hour larger (02:30:00) than the actual result (01:30:00). I tried setting time zones but it's always the same result.
Using the DateTime object, you can do this easily:
$d1 = new DateTime('2015-04-23 18:30');
$d2 = new DateTime('2015-04-23 20:00');
$interval = $d2->diff($d1);
echo $interval->format('%H:%i hours');