I tried to substract two datetimes to get result by seconds like that:
$created = "2015-01-16 07:26:55";
$newdate_created = date('Y-m-d H:i:s', strtotime('+2 month', strtotime($created)));
$now = date('Y-m-d h:i:s');
$interval = date_diff($now, $newdate_created);
$seconds = $interval * 60 * 60 * 12 ;
But i'm getting this error:
date_diff() expects parameter 1 to be DateTime
i dont know where is the problem to getting this working
I'm looking the output to be like that :
5259000 // something like that by seconds
Highly wasteful code. There is NO point in formatting your timestamps into strings, just to have to yank them back into timestamp format:
$newdate_created = strtotime('+2 month', strtotime($created));
$now = time();
$diff_in_seconds = $now - $newdate_created;
Related
So, I'm creating a booking system. When I retrieve this booking from the database, I need to check if the current date and time is closer to the actual booked date and time.
On the admin dashboard, the admins specify how much time earlier the client can make a checkin, let's say for example, 30minutes. But this time can be different. Can be 1hour, 2hours, 10minutes.
When I get the result from the database I get them like this:
$date_schedule = '2021-03-25 15:40:00'; // Can be any date in the future as well;
$time_to_check = '00:30:00'; // Can be '01:05:00', whatever the admins set as time_to_check;
// Expected result
'2021-03-25 15:10:00';
I tried subtracting this but I didn't made it work.. This is what I did.
$current_date = date("Y-m-d H:i:s");
$booking_date = '2021-03-25 15:00:00'; // From database
$time_to_check = '00:30:00'; // From database
$hours = explode(':', $time_to_check);
$data_check = date($booking_date, strtotime('-' . $hours[0] . ' hour -' . $hours[1] . ' minutes'));
But with this, $data_check returns the same value as $booking_date, it's not subtracting the time.
Convert your date to DateTime to make some operations on it :
function changeDate($date, $interval) {
$datetime = new DateTime($date);
$values = explode(':', $interval);
$datetime->modify("-$values[0] hours -$values[1] minutes -$values[2] seconds");
return $datetime->format('Y-m-d H:i:s');
}
$date = changeDate('2021-03-25 15:00:00', '00:30:00');
echo $date; // 2021-03-25 14:30:00
$date = changeDate('2021-03-25 15:00:00', '01:30:00');
echo $date; // 2021-03-25 13:30:00
$date = changeDate('2021-03-25 15:00:00', '02:30:15');
echo $date; // 2021-03-25 12:29:45
You can find documentation here https://www.php.net/manual/en/book.datetime.php
Just convert the timestamp to Unix time, and then subtract 30 minutes in seconds (30 * 60) from the Unix time.
Like this:
$date = '2021-03-25 15:10:00';
$timestamp = strtotime($date);
$timestamp = ($timestamp - (30 * 60));
echo gmdate("Y-m-d H:i:s", $timestamp);
EDIT: If you are in an odd timezone, like me (GMT+0100) add or subtract difference;
$timestamp = (($timestamp - (30 * 60)) + (1 * 60 * 60))
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 try to convert current date + 1 month to a final timestamp for Active Directory attribut accountExpire.
But the timestamp returned is wrong if someone can help me. Thanks
$now = strtotime(date('Y-m-d H:i:s'));
$final = date('Y-m-d H:i:s', strtotime('+1 month', $now));
$dateTime = new DateTime($final);
$timestamp = $dateTime->format('U');
echo $timestamp;
You only need
$timestamp = strtotime('+1 month');
I had to look it up, but it turns out that LDAP timestamp is not the same as UNIX timestamp. LDAP timestamp is counted in 100s of nanoseconds and is counted from year 1601. You can easily convert one into another by applying the difference and multiplying by 10000000
$UNIXtimestamp = strtotime('+1 month');
$LDAPTimestamp = ($UNIXtimestamp + 11644473600) * 10000000;
I have a date time in 'Y-m-d H:i:s', and i tried to substract the now date with the defined date +1 day to get remaining time in hours, minutes and seconds:
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
$dteDiff = $endTransaction - $now;
echo $dteDiff;
but i always get 0 as the result
You are doing it wrong. The date function returns string so PHP is not able to compare anything. Try with the DateTime class instead. Its diff method returns the DateInterval object with some public properties, like the days property among others, which is the positive integer number (rounded down) of days between two dates:
$now = new \DateTime();
$endTransaction = (new \DateTime('2017-12-05 14:54:03'))->modify('+1 day');
$diff = $endTransaction->diff($now);
printf(
'Difference in days: %d, hours: %d, minutes: %d, seconds: %d',
$diff->days,
$diff->h,
$diff->m,
$diff->s
);
You probably need to use this date_diff
$time = '2017-10-05 14:54:03';
$now = date_create(date('Y-m-d H:i:s'));
$endTransaction = date_create(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = date_diff($now, $endTransaction);
$date = new DateTime($dteDiff);
$result = $date->format('Y-m-d H:i:s');
According to above mentioned description please try executing following code snippet as a solution to it.
$time = '2017-10-05 14:54:03';
$now = strtotime(date('Y-m-d H:i:s'));
$endTransaction = strtotime(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = ($endTransaction - $now)/(24*60*60);
echo round($dteDiff);
$endTransaction and $now are strings.
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
echo($endTransaction."\n");
echo($now."\n");
It prints:
2017-10-06 14:54:03
2017-10-05 11:45:39
The subtraction is not a valid operation for strings. It can handle only numbers. The strings above are converted to numbers. The conversion uses only the leftmost digits present in the string, until it reaches the first character that is not a digit.
Both strings above produce 2017 when they are converted to numbers and their difference is, of course, 0.
The easiest way to work with dates in PHP is to use the DateTime and its related classes.
// Convert the input string to a DateTime object
$then = new DateTime('2017-10-05 14:54:03');
// Add 1 day
$then->add(new DateInterval('P1D'));
// Get the current date and time
$now = new DateTime('now');
// Compute the difference; it is a DateInterval object
$diff = $now->diff($then);
// Display the dates and the difference
echo('Then: '.$then->format("Y-m-d H:i:s\n"));
echo('Now : '.$now->format("Y-m-d H:i:s\n"));
echo('Remaining: '.$diff->format("%R%a days, %h hours, %i minutes, %s seconds.\n"));
The output:
Then: 2017-10-06 14:54:03
Now : 2017-10-05 12:36:25
Remaining: +1 days, 2 hours, 17 minutes, 38 seconds.
I am trying to add hh:mm:ss with the date. How can i do it?
I tried with the following but it works when the hour is string, but when adding time is similar to MySQL Date time it is not working.
$new_time = date("Y-m-d H:i:s", strtotime('+5 hours'));
I am trying to get solution for the following:
$timeA= '2015-10-09 13:40:14';
$timeB = '03:05:01'; // '0000-00-00 03:05:01'
OutPut:
$timeA + $timeB = 2015-10-09 16:45:15 ?
How Can I Add this?
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
print date('Y-m-d H:i:s',strtotime($timeA." +03 hour +05 minutes +01 seconds"));
Should work also.
So:
$timeA= '2015-10-09 13:40:14';
$timeB = vsprintf(" +%d hours +%d minutes +%d seconds", explode(':', '03:05:01'));
print date('Y-m-d H:i:s',strtotime($timeA.$timeB));
Can be the solution.
You may also convert the time into seconds with this approach from: Convert time in HH:MM:SS format to seconds only?
$time = '03:05:01';
$seconds = strtotime("1970-01-01 $time UTC");
Then you could add the seconds to
$currentTime = '2015-10-10 13:40:14';
$newTime = date("Y-m-d H:i:s", strtotime( $currentTime.'+'.$seconds.' seconds'));
If you prefer to use the DateTime objects offered by #John Conde, here are two ways to convert the time string into the format:
$formattedTime = preg_replace("/(\d{2}):(\d{2}):(\d{2})/","PT$1H$2M$3S","03:05:11");
or, as you read it from the database:
select concat(hour(last_modified),'H',minute(last_modified),'M',second(last_modified),'H') from people;
So a more general code approach would be:
$initial = 'some time';
$interval = 'the interval value';
$initialTime = new DateTime($initial);
$intervalTime = new DateInterval($interval);
$initialTime->add($intervalTime);
echo $initialTime->format('Y-m-d H:i:s');