While using strtotime function , if i am giving +48 day i am not sure whether is working fine or not ?
<?php
date_default_timezone_set('Asia/Kolkata');
$Seconds = 8604800 ;
$At = "2018-11-28 12:16:19";
echo date('Y-m-d H:i:s',strtotime("+48 day",strtotime($tAt)));
?>
strtotime expects the first parameter to be a valid time string. You are providing the number of seconds. Try -
echo $requestValidTill = date('Y-m-d H:i:s',strtotime("+$resetPasswordDurationInSeconds SECONDS",strtotime($requestAt)));
Output
2018-12-05 12:16:19
strtotime()
Working code
if you have PHP 5.3+ you can use the following lines of code
$requestAt = "2018-11-28 12:16:19";
$resetPasswordDurationInSeconds = 604800 ; //60 * 60 * 24 * 7 ( +7 days in seconds )
$date = new DateTime($requestAt );
$date->add(new DateInterval('PT'.$resetPasswordDurationInSeconds.'S')); // adds 604800 secs
echo date('Y-m-d H:i:s', $date->getTimestamp());
Your just need to add seconds in strtotime
<?php
$requestAt = strtotime("2018-11-28 12:16:19");
$requestAt += 604800;
echo date('Y-m-d H:i:s', $requestAt);
?>
Live Demo
Related
I am comparing a date with a datetime and I get the result I expect however I also am wondering if there is a better way to display my output and I have a query on my current output also.
Here is a snippet of my current code:
<?php
$todayDate = date('Y-m-d');
$seconds = strtotime($todayDate) - strtotime($dueDate);
$hours = $seconds / 60 / 60;
echo number_format($hours, 2);
?>
in my case $dueDate in my database here is 2017-06-26 09:11:28 so the output is displaying as -57.19. My question, is there is a clean way to strip the - and also add h after the hours and m after the minutes so the output looks like this?
57h 19m
UPDATE
So After tinkering around I have managed to do this:
substr($dateFormat,0,3).'h '.substr($dateFormat,4).'m';
The output now is -57h 19m
I still have this negative character, im not sure if that is actually correct I cannot seem to work it out because the date in my database is a day ahead but it shows a negative value...
Using the DateTime class makes it very simple
$dueDate = '2017-06-26 09:11:28';
$due = DateTime::createFromFormat('Y-m-d H:i:s', $dueDate);
$today = new DateTime();
$diff = $today->diff($due);
echo $diff->format('%hh %im');
Result:
11h 37m
But as you asked about timezones, here is how to add those in as well. And also as you orignial date was in fact some days distant I added a more accurate difference output
$dueDate = '2017-06-25 00:00:00';
$due = DateTime::createFromFormat('Y-m-d H:i:s', $dueDate, new DateTimeZone('Europe/London'));
$today = new DateTime('now', new DateTimeZone('Europe/London'));
$diff = $today->diff($due);
echo $diff->format('%R %hh %im').PHP_EOL;
if ( $diff->invert ) {
echo $diff->format('Overdue by %dd %hh %im');
} else {
echo $diff->format('You have %dd %hh %im till overdue');
}
Results
+ 1h 6m
You have 0d 1h 6m till overdue
You need to keep date integer
$time = time();
after
you can use this every where and evert way
For example
$date1 = time();
$date2 = time();
$comparingdate = $date2 - $date1;
$myFormat = date("T-m-d h:i:s",$comparingdate); // Show how you want
use floor and round functions to get the minutes and hours after convert the date to positive sign using abs function
<?php
$todayDate = date('Y-m-d');
$dueDate = "2017-06-26 09:11:28";
$seconds =abs(strtotime($todayDate) - strtotime($dueDate));
$hours =floor($seconds / 60 / 60);
$minutes= round($seconds / 60 / 60 - $hours,2)*100;
echo "<br>";
echo $hours. " H :";
echo $minutes. " M ";
?>
In my project time shows as
"2017-01-01 12:00:00+03:00".
How to make it as
"2017-01-01 15:00:00"
?
You can use like this
echo date("Y-m-d H:i:s A",strtotime('2017-01-01 12:00:00+03:00'));
But you will get 09:00:00 AM not 15:00:00 AM because your time is +3 ahead
https://eval.in/822865
Your time 2017-01-01 12:00:00+03:00 means that it is hour 12:00:00 in the timezone of +3 hours. You don't want to do anything just to reformat your time to different display:
(new DateTime($yourDateInStringOrUnix))->format('Y-m-d H:i:s');
and you will get
`2017-01-01 12:00:00` //This is local time in timezone of +3 hour
You want to add offset to your current time. Separate datetime with offset and recalculate it.
<?php
date_default_timezone_set('UTC');
$date = '2017-01-01 12:00:00+03:00';
$datetime = substr($date, 0, 19); // Get date time string
$offset = substr($date, 19); // Extract Offset
list($hours, $minutes) = explode(':', $offset);
$seconds = $hours * 60 * 60 + $minutes * 60; // Convert offset to seconds
// Recalculate date time by adding offset
echo date('Y-m-d H:i:s', strtotime($datetime) + $seconds);
?>
Output
2017-01-01 15:00:00
Hope this helps!
You can make it like this.
If this is coming from your data you can do it like this:
$my_date = "2017-01-01 12:00:00+03:00";
$converted_date = date('Y-m-d H:i:s', strtotime($my_date));
echo $converted_date;
But if you are getting the current date and time only you can make it this way;
$my_date = date('Y-m-d H:i:s');
echo $my_date;
I want to send a reminder email.I don't want to use cron on Linux/Unix/BSD box or Scheduled Tasks on Windows.
I'm trying to subtract 15 minutes from the current time.
here is my code so far (doesn't work):
$days = date("j",time());
$months = date("n",time());
$years = date("Y",time());
$hours = date("G",time());
$mins = (date("i",time()));
$secs = date("s",time());
$mins = $mins-15;
To subtract 15 minutes from the current time, you can use strtotime():
$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);
Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
You can use DateInterval
$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
you can try this as well,
$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
How about substracting the 15 minutes from time() before converting it?
$time = time() - (15 * 60);
And then use $time instead of time() in your code.
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
You can also use strtotime function to subtract days, hours and/or seconds from current time.
echo date('Y-m-d H:i:s', strtotime('-15 minutes'));
Following is the way you can add days / hours / minutes / sec to current time
$addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));
You can also use DateInterval object
<?php
$date = new DateTime('Y-m-d H:i:s');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s');?>
Try using
$min = time() - 900; //900 seconds = 15 minutes
To subtract 15 minutes you can do:
date('Y-m-d H:i:s', (time() - 60 * 15));
You can replace 15 with the number of minutes you want.
In case you're looking to subtract seconds you can simply do:
date('Y-m-d H:i:s', (time() - 10));
In this way you'll subtract 10 seconds.
If you have only time value than below will be useful
// Your time
$time = '12:15:00';
// Returned value '12:00:00'
$newTime = date('H:i:s', strtotime($time) - (15*60));
I know this question is outdated but i just want to share how i did it in easy way
$current = new DateTime("10 minutes ago", new DateTimeZone('Asia/Manila') );
echo $current->format("Y-m-d H:i:s");
//To Get Current DateTime
$currentDate = date("Y-m-d H:i:s");
//To Get Current DateTime - 15Min
$oldDate = date("Y-m-d H:i:s", strtotime($currentDate) - (15 * 60));
echo $currentDate;
echo $oldDate;
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.
how can I find the next closest hour in php
so for example if current time is 4:15 the next hour will be 5, etc
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );
gives me the time from the string and I want to expand on that and get the next closest hour
$nextHour = (intval($date->format('H'))+1) % 24;
echo $nextHour; // 5
Here we go:
<?php
echo date("H:00",strtotime($date. " + 1hour "));
?>
Can you just take pieces (hours, minutes, seconds) and get the next hour?
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );
echo "\n";
$nexthour = ($date->format('H') + ($date->format('i') > 0 || $date->format('s') > 0 ? 1 : 0)) % 24;
echo "$nexthour:00:00";
Supply any eligible date() to:
function roundToNextHour($dateString) {
$date = new DateTime($dateString);
$minutes = $date->format('i');
if ($minutes > 0) {
$date->modify("+1 hour");
$date->modify('-'.$minutes.' minutes');
}
return $date;
}
<?php
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
$date->modify('+1 hour');
echo $date->format('H:i:s').PHP_EOL;
// OR
echo date('H:i:s', strtotime($dateString) + 60 * 60).PHP_EOL;
As I just needed something similar (next full hour) here my solution:
$now = time();
$nextFullHour = date(DATE_ATOM, $now + (3600 - $now % 3600));
By replacing the 3600 e.g. with 60 you get the next full minute...
You can also replace the $now with any other timestamp if you do not need it relative to the current time.
That is my solution:
$dateTime = new \DateTime();
$dateTime->add(new \DateInterval('PT1H'))
->setTime($dateTime->format('H'), '00');
Nobody else used this one so I figured I'd drop it here, simplest one I saw above for an actual timestamp, not just the hour itself.
$now = ''; // empty uses current time, or you can insert a datetime string
$next_hour = date( 'Y-m-d H:00:00', strtotime( $now . ' +1 hour' ) );
try it for current time, if you need put second argument to date function
<?php echo date('H')+1; ?>
very nice stuff
One more:
$current_datetime = new DateTimeImmutable();
$next_full_hour_datetime = $current_datetime
->modify(
sprintf(
'+%d seconds',
3600 - ($current_datetime->getTimestamp() % 3600)
)
);
A little late to this party, but here's a more flexible function to round up a dateTime object by any interval in minutes. You pass in your dateTime object and a rounding interval in minutes, so for an hour you'd just pass in 60, etc.
public function round_up_time( $datetime, $rounding_interval ) {
// get total minutes from the start of the day
$minutes = ( intval( $datetime->format( 'H' ) ) * 60 ) + ( intval( $datetime->format( 'i' ) ) );
// round up the minutes based on the interval we are rounding to
$rounded_minutes = ( intval( $minutes / $rounding_interval ) + 1 ) * $rounding_interval;
// reset our dateTime to the very start of the day
$datetime->setTime( 0, 0 );
// then increase the dateTime by the rounded minutes value
$datetime->modify( '+ ' . $rounded_minutes . ' minutes' );
}
To get the next closest full hour in DateTime:
$date = new DateTime('+1 hour'); //set the next hour
$date->setTime($date->format('H'), '00', '00'); //keep the next hour, set minutes to 00 and seconds to 00