PHP Current time in Hour:Minutes - php

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

Related

how to get date after 2 days from the given time in php

i want to find out the date after days from the given time.
for example. we have date 29 may 2015
and i want to cqlculate the date after 2 days of 25 may 2015
$Timestamp = 1432857600 (unix time of 29-05-2015)
i have tried to do it with following code but it is not working
$TotalTimeStamp = strtotime('2 days', $TimeStamp);
Missed the + - strtotime('2 days', $TimeStamp); .
Add the + to + 2 days.
Use date & strtotime for this - You can try this -
echo date('d-m-Y',strtotime(' + 2 day', strtotime('2015-05-16')));
$Timestamp & $TimeStamp are not same(may be typo). For your code -
$Timestamp = strtotime(date('Y-m-d'));
$TotalTimeStamp = strtotime('+ 2 days', $Timestamp);
echo date('d-m-Y', $TotalTimeStamp);
Php does have a pretty OOP Api to deal with date and time.
This will create a \DateTime instance using as reference the 25 May 2015 and then you can call the modify method on that instance to add 2 days.
$date = new \DateTime('2015-05-25');
$date->modify('+2 day');
echo $date->format('Y-m-d');
You may find this resource useful:
http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395
You can also just add seconds to your timestamp if you have a timestamp ready:
$NewDateStamp = $Timestamp + (60*60*24 * 2);
In the above, sec * min * hours = day -- or 86400 seconds. * 2 = 2 days.
In PHP 5 you can also use D
<?php
$date = date_create('2015-05-16');
date_add($date, date_interval_create_from_date_string('2 days'));
echo date_format($date, 'Y-m-d');
?>
OR
<?php
$date = new DateTime('2015-05-16');
$date->add(new DateInterval('2 days'));
echo $date->format('Y-m-d') . "\n";
?>

Calculating time difference for fixed hour

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');

How to add timestamp to Minutes in php

$date = 10/24/14
$time = 10:10
$mtc = 180 minutes
After using strtotime function i am getting timestamp,
echo strtotime('10/24/14 10:10');
timestamp is 1414138200
How can i add timestamp to 180 minutes. Can any one please help me.
If i'm understand your question well, you just can add the seconds to the current timestamp.
$timestamp = time();
$timestamp = $timestamp + 60*180; //180 minutes later
Unix timestamp is expresses in seconds. So to add 180 mins to it, just convert to secs and add to the current timestamp like
$ts = strtotime('10/24/14 10:10');
$ts += 180*60;
echo date("m/d/Y H:i",$ts);
Try this on
$time = strtotime('10/24/14 10:10');
$afterAddMinutesTime = strtotime('+180 minutes', $time);
echo $afterAddMinutesTime; // this is new timestamp after add 180 minutes
echo date('Y-m-d H:i:s', $afterAddMinutesTime); // just for checking

Check how many days were passed since the last update in PHP

I am trying to check how many days were passed since the user last entered the system. I get the last time he\she entered from mysql table column (datetime). so I wrote :
$user_last_visit = $user_info['last_updated']; // 2013-08-08 00:00:00
$user_last_visit_str = strtotime($user_last_visit); // 1375912800
$today = strtotime(date('j-m-y')); // 1250114400
$diff = $today - $user_last_visit_str;
Where $user_info['last_updated'] has the last time he\she visited with the value of 2013-08-08 00:00:00.
After strtotime I get $user_last_visit_str equals to 1375912800
$today has the value of 9-08-13 and after strtotime I get 1250114400.
Some reason $diff = $today - $user_last_visit_str; is negative instead of getting a positive value with 24*60*60*1000 (one day = 24*60*60*1000 ms).
Any ideas?
A simple solution using diff:
echo date_create()->diff(date_create($user_last_visit))->days;
If all else fails, just do:
$diff = floor((time() - $user_last_visit_str) / (60 * 60 * 24));
you can use below code to get date diff, here i given static last date which is 15th july 2013, and taking different from current date.
$last_date = date('y-m-d', strtotime('15th july 2013'));//here give your date as i mentioned below
//$last_date = date('y-m-d', strtotime($your_old_date_val));
$current_date = date('y-m-d');//
echo $date_diff = strtotime($current_date) - strtotime($last_date) ;
echo $val = 60*60*24;
$days_diff = $date_diff / $val;
echo $days_diff;
Try this:
$user_last_visit = $user_info['last_updated']; // considering the value of user last visit is 2013-08-08 00:00:00 which indicates year, month, day, hour, minute, second respectiveliy
$user_last_visit_str = strtotime($user_last_visit);
$today = time();
$diff = $today - $user_last_visit_str;
$no_of_days = ($diff / (60*60*24));
Try using a DateTime object if your version of PHP supports it:
$user_last_visit = DateTime::createFromFormat('Y-m-d H:i:s', $user_info['last_updated']);
$today = new DateTime();
$diff = $today->diff( $user_last_visit, true );
echo $diff->days;
$diff will be a DateInterval object.
try reversing the $today assignment, like so:
$today = strtotime(date('y-m-j'));
that worked for me.

convert time in php

I have the data 2013-02-04 03:20:00
How do I arrive into this 07:20 using php.
Same is through with this 2013-02-04 08:00:00 to this 12:00
I have this data labeled timestamp_diff which is 14400
Any idea? Thank You
I think the answer was base on the timestamp_diff not just adding 4 or any values to it.
Is there any datetime() function(s) to get the equivalent of 14400 to 4
<?php
$add4hour = time() + (4*60*60);
$newTime = date("d m Y H:i:s",$add4hour);
echo $newTime;
?>
Edit because of your requirements
<?php
$add4hour = time() + (4*60*60); // 4 hour adding
$d1 = date("d-m-Y H:i:s"); // date 1 assume that hour 03:20:00
$d2 = date("d-m-Y H:i:s",$add4hour); // date 2, Assume that hour 07:20:00 by adding 4hour
$d1_timestamp = strtotime($d1); // first date's timestamp
$d2_timestamp = strtotime($d2); // second date's timestamp
$time_diff = $d2_timestamp - $d1_timestamp; //difference
echo $time_diff; // this will give you 14400
?>
$date = date("Y-m-d H:i:s");
$addhr= 4;//Replace you value to be added here
$format="H:i";
echo $date."<br>";
echo date($format, strtotime("$date + $addhr hours"));
You can use \DateTime class and add any interval with DateTime::add. Also you can use procedure style.

Categories