Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtract 6 hours from date('g:i a', strtotime($time_date_data));

Tags:

date

php

I am using the following code to transform a universal time code into something a little more user friendly.

$meeting_time = date('g:i a', strtotime($time_date_data));

But now I need to subtract 6 hours from meeting_time. Should I do it after the code above or can I work it into the same date function?

Something like:

$meeting_time = date('g:i a' - 6, strtotime($time_date_data));
like image 923
Denoteone Avatar asked Nov 21 '11 15:11

Denoteone


People also ask

How do I convert Strtotime to date format?

Code for converting a string to dateTime $date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

What does Strtotime mean in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How do you add hours on Strtotime?

You can use DateTime::modify to add time, but I would just do time()+10800 . Show activity on this post. $time = new DateTime("+ 3 hour"); $timestamp = $time->format('Y-M-d h:i:s a');

How do I use Strtotime?

If you want to use the PHP function strtotime to add or subtract a number of days, weeks, months or years from a date other than the current time, you can do it by passing the second optional parameter to the strtotime() function, or by adding it into the string which defines the time to parse.


1 Answers

$meeting_time = date('g:i a', strtotime($time_date_data) - 60 * 60 * 6);

String-to-time (strtotime) returns a Unix Time Stamp which is in seconds (since Epoch), so you can simply subtract the 21600 seconds, before converting it back to the specified date format.

like image 97
danielrsmith Avatar answered Oct 18 '22 21:10

danielrsmith