I'm getting a time range like this from a field in WordPress:
8 am - 5 pm
More than anything I just want to make sure that I'm thinking about the correctly.
I'm assuming that if I want to convert that time to 24 hour format, I'll need to strip out the dash - first and then convert each hour 8 am and 5 pm to be individual time numbers. So something like this?:
$newHours = explode('-',$hours);
$time_am = date("H:i", strtotime($newHours[0]));
$time_pm = date("H:i", strtotime($newHours[1]));
Wondering if there is a possible better way of going about that especially if I'm getting hours for multiple days as well.
You can try something like this:
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));
Related
I need to convert 4 digit times to am/pm and can do that using this function: date("g:ia",$starttime) which converts 08:30 to "8:30am".
But I want times that are on the hour to hide the zero minutes. So I want 09:00 to be displayed as "9am". Is there something built into php to easily do this?
$date = date("g:ia",$starttime);
$date = str_replace(":00", "", $date);
I want to add a minute to the time which is the post posted
Let say that $time_posted = "12:14" where 12 is hours and 14 minutes, what i actualy want is to add 1 minute to the $time_posted
NOTE: $time_posted is for different posts different.
Thanks in advance.
echo date("H:i", strtotime("12:14") + 60);
Change the H depending on what you need (look at the values for that here or below). I chose H because I assumed it's a 24-hour clock with leading zeros, but you may change it to:
g: 12-hour format of an hour without leading zeros 1 through 12
G: 24-hour format of an hour without leading zeros 0 through 23
h: 12-hour format of an hour with leading zeros 01 through 12
H: 24-hour format of an hour with leading zeros 00 through 23
The 60 indicates 60 seconds (or 1 minute).
You can use PHP's built-in function mktime for this. With this, you can add or subtract from any part of a date by just using a plus (or minus) sign after the part you want to change. Here's an example of adding 1 to the minute part of a time:
$time_posted = '12:14';
// SPLIT APART THE HOUR AND MINUTE
list($hour, $minute) = explode(':', $time_posted);
$new_date = date("H:i", mktime($hour, $minute + 1, 0, date("m"), date("d"), date("Y")));
print $new_date;
This will output: 12:15
Here is a working demo
Edit:
I just saw that the time format can be in different formats. I don't really know what to tell you there other than you need to find a way to normalize the data. You should never rely on users to input whatever they want. Obviously this code will not work if it can't parse out hour and minute from the timestamp. You'd need to write a complex REGEX to search for all possible combinations of user-supplied combinations. Not something you want to do normally.
I've set the timezone on my website pages as such:
date_default_timezone_set("America/Los Angeles");
I've declared a time variable as such:
$time = date("h:i:sa");
then adding it to the database. I then retrieve the time from the database like such, trying to reformat it:
$time2 = date("h:i A", strtotime($row['Time']));
and then printing out the time:
echo $time2;
This method, however, messes up AM and PM. If $time was added to the database at 11:59:00 at night, for example, it will print out 11:59 AM, not 11:59 PM. I think the issue is that when the time is added to the database, no clear distinction is made between AM and PM. However, I'm still unsure how to fix this. I've tried adding the time with the AM PM already in it, like such:
$time = date("h:i:sa A");
and then accessing it like such:
$time2 = $row['Time'];
but this still doesn't fix the bug. Is there a way around this while keeping my 12 hour format? Or do I have to change to 24 hour format to get this to work?
Please start storing dates/times in Y-m-d/H:i:s format in DATETIME, DATE or TIME fields. Your life, and ours, would be much easier.
Until then, use this PHP solution:
$dt = DateTime::createFromFormat('!h:i:sa', $row['Time']);
echo $dt->format('H:i:s');
demo
or mysql solution:
SELECT STR_TO_DATE('08:46:07am','%h:%i:%s%p')
If I got a unix time which is e.g
1407050129
How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.
Example if i want get today first minute
$today_first_min = strtotime("00:00:00");
Try this:
$that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.
Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!
date_default_timezone_set('UTC');
$that_date = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));
print date('Y-m-d H:i:s', strval($first_hour));
// 2014-08-03 00:00:00
$eve['start']['dateTime'] = 2013-05-02T14:00:00+05:30;
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
$start_time = date("H:i A",strtotime($eve['start']['dateTime']));
when i use the above code am getting it as 05-02-2013 08:30 AM
But i should get 05-02-2013 2:00 PM
why this time difference and shows wrong any idea?
The time difference is not wrong. You are getting the correct date and time for a timezone at +0:00. To fix this, set your timezone.
your formatters seems to be incorrect, to get the desired output use the code below. H is used for 24 hour format with leading zero. h is used for 12 hour format.
date("h:i A");
$eve['start']['dateTime'] = "2013-05-02T14:00:00+05:30"; // Missing quotes in your code?
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
Should return 05-02-2013 08:30 AM because your server has timezone sat to GMT+0. If you take 14:00 and subtracts with 5 and a half hour (from +0530 to +0000), it should be 08:30.
To avoid this, you have to set default timezone on your server or in your script.