-3

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.

gview
  • 14,876
  • 3
  • 46
  • 51
Hamza
  • 721
  • 8
  • 8
  • Is the value time_posted value coming from a database? What is the datatype of the value in the database, and as a php variable? – gview Jan 14 '15 at 20:24
  • Have you tried anything? Any "code" to share? – Funk Forty Niner Jan 14 '15 at 20:28
  • You don't have a time value. You have an interval. Those are easily corrupted by strtotime, e.g. `var_dump(strtotime('25:53'))` gives you `bool(false)`, because `25:53` is NOT a valid time. – Marc B Jan 14 '15 at 20:30
  • @MarcB: How do we know it's an interval from that, ummm, verbiage. – AbraCadaver Jan 14 '15 at 20:33

2 Answers2

1
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).

Community
  • 1
  • 1
Shahar
  • 1,687
  • 2
  • 12
  • 18
0

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.

Quixrick
  • 3,190
  • 1
  • 14
  • 17