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.
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'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"));
Could somebody please take a look into my code:
$godzinaRozpoczecia = "10:35";
$dateStampGodzinaRozpoczecia = strtotime($godzinaRozpoczecia);
echo date("H:m", $dateStampGodzinaRozpoczecia);
RESULT: 10:08
var_dump($godzinaRozpoczecia);
RESULT: string(5) "10:35"
What is wrong? Why do I have minutes missing? In var_dump everything seems to be fine. The same issue occurs when I retrieve the time from the database.
The date() function specifies the following format for "H:m":
H: 24-hour format of an hour with leading zeros
m: Numeric representation of a month, with leading zeros
So you're actually showing the month here, you're looking for i:
i: Minutes with leading zeros
You may have been confused with the format used for the strtotime(), DateTime and date_create() formats. They are different from the date() formats (for some illogical reason).
Try this
date("H:i", $dateStampGodzinaRozpoczecia);
//m is for month.current month is August that is only it gives you 08 as aswer
I am working on project (a Google Transit feed) where I am required to provide the times for each stop on a bus route in the following common format: 21:00:00 and so forth.
Problem is, if times continue past midnight for a given trip, they require it to continue the hour counting accordingly. They explain quite specifically that 02:00:00 should become 26:00:00 and 03:45:00 should become 27:45:00 etc.
I am baffled on how to display such with any of the date() or strtotime() functions.
The only thing I can think of in my particular situation would be to function match and replace any strings in my output between 00:00:00 and 04:00:00, as that would clearly mean (again, for me only) that these are trips originating before midnight, but I don't feel that's the correct way.
Well seeing as it's only displaying on the page, you can
firstly get your date from where ever
Let's say $date = 00:00:00
$exploded_date = explode(":", $date);
This takes $date and puts it into an array so
$exploded_date[0] is hh
$exploded_date[1] is mm
$exploded_date[2] is ss
Then what you can do is use ltrim() to remove the leading 0 from 00 to 04 $exploded_date[0] - This makes it comparable in the if statement I'll do after
if($exploded_date[0] <= 4) {
$exploded_date[0] = ltrim($exploded_date[0], "0");
$exploded_date[0] = $exploded_date[0]+24;
}
Then you can implode the array back together into one string
$date = implode(":", $exploded_date);
// if the hour is 00 to 04 it will come out as 24 to 28
// e.g. 24:35:30
echo $date;
Despite giving you an answer. It's a silly thing to be doing, but it's not your choice so here you go :)
The way you display something doesn't necesarily has to be the same way you store something.
I don't know how you calculate the times, but assuming you have a start date and time, and some interval, you could calculate the end time as follows:
date_default_timezone_set('Europe/London');
$start_datetime = new DateTime('2014-11-11T21:00:00');
$next_stop = new DateTime('2014-11-12T02:00:00');
echo $start_datetime->format('Y-m-d H:i'); // 2014-11-11 21:00
echo $next_stop->format('Y-m-d H:i'); // 2014-11-12 02:00
$interval = $start_datetime->diff($next_stop);
// display next stop: 2014-11-11 26:00
echo ($start_datetime->format('Y') + $interval->y) .'-'
. ($start_datetime->format('m') + $interval->m) .'-'
. ($start_datetime->format('d') + $interval->d) .' '
. ($start_datetime->format('H') + $interval->h) .':'
. ($start_datetime->format('i') + $interval->i);
What I'm doing: create the start date (& time) and the datetime of the next stop. With the DateTime::diff() function I'm calculating the difference, and then, only for display (!) I add up each year, month, day, hour and minute to the datetime year, month etc. of the next stop.
This way you can still store your dates and times in a way every human being and computer system will understand (because let's be honest; to represent a time as 27:45 PM is quite ridiculous...)
I don't know if you only want the hours to be added up and roll over the 24 hour, or also days in a month etc. It's up to you how you handle these cases. Good luck!
Please check the following examples:
$date1 = strtotime("tomorrow 4:00 PM");
$date2 = strtotime("16:00:00");
$date3 = strtotime("10 hours");
$date4 = strtotime("+1 day");
echo date("Y m d H:i:s",$date1)."<br>";
echo date("Y m d H:i:s",$date2)."<br>";
echo date("Y m d H:i:s",$date3)."<br>";
echo date("Y m d H:i:s",$date4)."<br>";
It gives me the output as below:
2013 06 10 16:00:00
2013 06 09 16:00:00
2013 06 09 20:50:25
2013 06 10 10:50:25
I am considering first two example($date1 and $date2) as absolute data and the last two as relative date. Now, with only given the $date1/$date2/$date3/$date4 variables, is it possible to say whether it is relative time or an absolute time please?
I did get a solution on another thread: PHP datetime string differentiation
But that worked until I considered the 2nd example($date2 as an absolute value), where it doesn't work. Also, may suggested for regular expression checks, but that doesn't seem reliable either.
I was just wondering if php had some integrated way to tell this either from its functions or DateTime objects. I searched for, but didn't found anything.
Looking forward to listen for your suggestions/feedbacks/possible solutions. Thanks.
There is no direct way, AFAIK but there is a trick that you can use with the second parameter to the strtotime function.
function is_absolute_time($time_string) {
$time_shift = time() + 60; // 1 min from now
$time_normal = strtotime($time_string);
$time_shifted = strtotime($time_string, $time_shift);
return $time_normal == $time_shifted;
}
The rationale is simple: If the time is absolute, a 1 min difference won't change the calculation by strtotime and both $time_normal and $time_shifted will be same. For relative times, however, the difference will be one minute (the value in $time_shift variable).
There is a caveat with this code though. This function will return FALSE even for absolute times (but not absolute dates) less than 1 minute from midnight. You can minimize this by changing $time_shift to:
$time_shift = time() + 5; // 5 seconds from now.
This code will now work properly until 5 seconds from midnight. I think you can go safely to as low as 2. There is an edge case that 1 second in future might not work.
To fix this problem altogether, you can try a different approach:
function is_absolute_time($time_string) {
$epoch = 0; // Epoch
$time_shift = 60; // 1 min from epoch
$time_normal = strtotime($time_string, $epoch);
$time_shifted = strtotime($time_string, $time_shift);
return $time_normal == $time_shifted;
}
You can try this last solution directly. I am just building up the reason for the solution throughout this post.
Given only the value 2013 06 10 16:00:00 the answer is simple: it's absolute. Whether this absolute timestamp was created as "absolute" timestamp or based on relation to another date is impossible to tell. All you have is "2013 06 10 16:00:00", there's no "residual relativeness" or anything of that kind still in it.
Even this is relative to the supposed birth of Christ though, which is relative to the earth floating around in space since the Big Bang... *trollface*