PHP: add seconds to a date
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();
?>
Add 30 seconds to the time with PHP
$time = date("m/d/Y h:i:s a", time() + 30);
PHP Add Seconds
Try adding seconds to time()
$today = date( "m/d/Y h:i:s A", time() + 3600 ); // + 1 hour
Adding one microsecond to Datetime object in PHP
PHP >= 7.1 - works but there's a bug!
If you have PHP 7.1 or later then this should do it:
$date = new Datetime('2018-06-05 09:06:46.7487');
$date->modify('+1 microsecond');
echo $date->format('Y-m-d H:i:s.u');
Output:
2018-06-05 09:06:46.748701
Caution: this fails for .999999
$date = new Datetime('2018-06-05 09:06:46.999999');
$date->modify('+1 microsecond');
echo $date->format('Y-m-d H:i:s.u');
Output:
2018-06-05 09:06:46.1000000
PHP >= 5.2.0 "hack" but not buggy!
If you have PHP 7.0 or earlier then you can extract the microseconds and perform the math yourself in a "hacky" way:
$date = new Datetime('2018-06-05 09:06:46.7487');
// Use bcadd() to add .000001 seconds to the "microtime()" of the date
$microtime = bcadd( $date->getTimestamp().'.'.$date->format( 'u' ), '.000001', 6 );
// Reconstruct the date for consumption by __construct
$date->__construct(
date( 'Y-m-d H:i:s.', explode( '.', $microtime )[ 0 ] ).explode( '.', $microtime )[ 1 ]
);
echo $date->format('Y-m-d H:i:s.u');
Output:
2018-06-05 09:06:46.748701
The hacky solution also works if the microsecond is at .999999
$date = new Datetime('2018-06-05 09:06:46.999999');
// Use bcadd() to add .000001 seconds to the "microtime()" of the date
$microtime = bcadd( $date->getTimestamp().'.'.$date->format( 'u' ), '.000001', 6 );
// Reconstruct the date for consumption by __construct
$date->__construct(
date( 'Y-m-d H:i:s.', explode( '.', $microtime )[ 0 ] ).explode( '.', $microtime )[ 1 ]
);
echo $date->format('Y-m-d H:i:s.u');
Output:
2018-06-05 09:06:47.000000
add hours:min:sec to date in PHP
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
add seconds to a date using php?
Thanks all,This is my answer.
<?php
$WaitingTime=1423;
$strat_time="11/19/2014 2:49:23";
$dateinsec=strtotime($strat_time);
$newdate=$dateinsec+$WaitingTime;
$chat_start=date("Y-m-d H:i:s", $dateinsec);
$chat_end= date('Y-m-d H:i:s',$newdate);
$duration = sprintf('%02d:%02d:%02d', ($WaitingTime / 3600), ($WaitingTime / 60 % 60), $WaitingTime % 60);
<?
Add seconds to date for time feedback
Basically you're looking for if the time it was posted is less than five seconds ago. Easy, but you MUST use timestamps. Formatted times are no good.
relativeTime($arrSchool['updated']); // if it's a numeric timestamp
relativeTime(strtotime($arrSchool['updated'])); // if it's a datetime string
function relativeTime($timestamp) {
if( $timestamp > time()-5) {
echo "Updated Just Now";
}
else {
echo "not now";
}
}
Related Topics
Trying to Pass Todataurl with Over 524288 Bytes Using Input Type Text
How to Get Wordpress Post Featured Image Url
Linux - PHP 7.0 and Mssql (Microsoft SQL)
PHP Warning: Unknown: Failed to Open Stream
Laravel Gmail Not Working, "Username and Password Not Accepted. Learn More..."
Differencebetween Switch-Case and If-Else in PHP
Get Root Directory Path of a PHP Project
Html5 Audio Says "Live Broadcast" in iOS When It's a Static File
Remove First 4 Characters of a String with PHP
Php: Determine Where Function Was Called From
Starting with Laravel on Ubuntu
How to Expose All the Acf Fields to Wordpress Rest API in Both Pages and Custom Postypes