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.
Related
I currently have php returning the current date/time like so:
$now = date("Y-m-d H:m:s");
What I'd like to do is have a new variable $new_time equal $now + $hours, where $hours is a number of hours ranging from 24 to 800.
Any suggestions?
You may use something like the strtotime() function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).
If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours)) then.
An other solution (object-oriented) is to use DateTime::add
Example:
<?php
$now = new DateTime(); //now
echo $now->format('Y-m-d H:i:s'); // 2021-09-11 01:01:55
$hours = 36; // hours amount (integer) you want to add
$modified = (clone $now)->add(new DateInterval("PT{$hours}H")); // use clone to avoid modification of $now object
echo "\n". $modified->format('Y-m-d H:i:s'); // 2021-09-12 13:01:55
Run script
DateTime::add PHP doc
DateInterval::construct PHP doc
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
Correct
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', strtotime($now))); // $now + 3 hours
You can also use the unix style time to calculate:
$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";
Um... your minutes should be corrected... 'i' is for minutes. Not months. :) (I had the same problem for something too.
$now = date("Y-m-d H:i:s");
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
I use this , its working cool.
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
for add 2 hours to "now"
$date = new DateTime('now +2 hours');
or
$date = date("Y-m-d H:i:s", strtotime('+2 hours', $now)); // as above in example
or
$now = new DateTime();
$now->add(new DateInterval('PT2H')); // as above in example
You can try lib Ouzo goodies, and do this in fluent way:
echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");
API's allow multiple operations.
For a given DateTime, you can add days, hours, minutes, etc. Here's some examples:
$now = new \DateTime();
$now->add(new DateInterval('PT24H')); // adds 24 hours
$now->add(new DateInterval('P2D')); // adds 2 days
PHP: DateTime::add - Manual https://www.php.net/manual/fr/datetime.add.php
$to = date('Y-m-d H:i:s'); //"2022-01-09 12:55:46"
$from = date("Y-m-d H:i:s", strtotime("$to -3 hours")); // 2022-01-09 09:55:46
$date_to_be-added="2018-04-11 10:04:46";
$added_date=date("Y-m-d H:i:s",strtotime('+24 hours', strtotime($date_to_be)));
A combination of date() and strtotime() functions will do the trick.
$now = date("Y-m-d H:i:s");
date("Y-m-d H:i:s", strtotime("+1 hours $now"));
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
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";
?>
I have the current date format $date = 'yyyy-mm-dd hh:mm' and notice that $date is already set and not using the Date class. I understand I have to use strtotime which I have done to my $date $datestr = strtotime($date). I wish to subtract 5 minutes to the set time.
I've tried this simple thing $A = strtotime($date) - strtotime('-5 min'); But it does not aid me.
its simple to subtract the seconds of 5 minutes which is (5*60)=300 from your time
like this
$time = strtotime($date);
$time_new = $time-(5*60); // will time of the -5 min from the current $time
example
$date = date('d-M-Y g:i:s A'); // current date
echo $date."<br/>"; // output : 17-Feb-2014 8:35:58 AM
$time = strtotime($date); // convert current date to timestamp
$time_new = $time - (5*60); // subtract 5 minutes from current time.
$date_new = date('d-M-Y g:i:s A', $time_new); // convert time to the date again
echo $date_new; // output : 17-Feb-2014 8:30:58 AM
I have $adate; which contains:
Tue Jan 4 07:59:59 2011
I want to add to this date the following:
$duration=674165; // in seconds
Once the seconds are added I need the result back into date format.
I don't know what I'm doing, but I am getting odd results.
Note: both variables are dynamic. Now they are equal to the values given, but next query they will have different values.
If you are using php 5.3+ you can use a new way to do it.
<?php
$date = new DateTime();
echo $date->getTimestamp(). "<br>";
$date->add(new DateInterval('PT674165S')); // adds 674165 secs
echo $date->getTimestamp();
?>
Just use some nice PHP date/time functions:
$adate="Tue Jan 4 07:59:59 2011";
$duration=674165;
$dateinsec=strtotime($adate);
$newdate=$dateinsec+$duration;
echo date('D M H:i:s Y',$newdate);
Given the fact that $adate is a timestamp (if that's the case), you could do something like this:
$duration = 674165;
$result_date = strtotime(sprintf('+%d seconds', $duration), $adate);
echo date('Y-m-d H:i:s', $result_date);
// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));
Do this:
$seconds = 1;
$date_now = "2016-06-02 00:00:00";
echo date("Y-m-d H:i:s", (strtotime(date($date_now)) + $seconds));
$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
I made this example for a timezone, but if you change some parts it may help you out:
$seconds_to_add = 30;
$time = new DateTime();
$time->setTimezone(new DateTimeZone('Europe/London'));
$time2 = $time->format("Y/m/d G:i:s");
$time->add(new DateInterval('PT' . $seconds_to_add . 'S'));
$timestamp = $time->format("Y/m/d G:i:s");
echo $timestamp;
echo '========';
echo $time2;
Result:
2018/06/17 3:16:23========2018/06/17 3:15:53
It would be easier with DateTime::modify
(new DateTime($str))->modify("+$duration seconds"); //$str is the date in string
I have trouble with strtotime() to resolve my problem of add dynamic data/time value in the current time
This was my solution:
$expires = 3600; //my dynamic time variable (static representation here)
$date = date_create(date('Y-m-d H:i:s')); //create a date/time variable (with the specified format - create your format, see (1))
echo date_format($date, 'Y-m-d H:i:s')."<br/>"; //shows the date/time variable without add seconds/time
date_add($date, date_interval_create_from_date_string($expires.' seconds')); //add dynamic quantity of seconds to data/time variable
echo date_format($date, 'Y-m-d H:i:s'); //shows the new data/time value
font: https://secure.php.net/manual/en/datetime.add.php (consult Object Oriented style too, the Elzo Valugi solution)
(1) https://secure.php.net/manual/en/function.date.php