0

How would i convert a format like:

13 hours and 4 mins ago

Into something i can use in php to further convert?

3
  • strtotime() seems to fail... The best way would not to have to parse natural language in the first place. Where is this coming from? No chance of getting a more machine-readable format? Commented Feb 22, 2011 at 18:49
  • it's to bad users can't go negative in their rep. Commented Feb 22, 2011 at 18:49
  • @Pekka so what? Doesn't 5 or even 2 questions deserve at least a sign of gratitude? Commented Feb 22, 2011 at 19:17

5 Answers 5

2

try

strtotime('13 hours 4 mins ago', time())

http://php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/datetime.formats.php

http://www.php.net/manual/en/datetime.formats.relative.php

Sign up to request clarification or add additional context in comments.

Comments

0

To get a unix timestamp fromt he current server time: $timestamp = strtotime("-13 hours 4 minutes");

Comments

0

Try the DateInterval class - http://www.php.net/manual/en/class.dateinterval.php - which you can then sub() from a regular DateTime object.

1 Comment

DateInterval is nice, but I doubt it can parse the input string the OP is giving
0

Do it the hard way:

$offset = (time() - ((60 * 60) * 13) + (4 * 60));

Working Example: http://codepad.org/25CJPL76

Explanation:

(60 * 60) is 1 hour, then * 13 to make 13 hours, plus (4 * 60) for the 4 minutes, minus the current time.

What i usually do is create several constants to define the values of specific time values like so:

define("NOW",time());
define("ONE_MIN", 60);
define("ONE_HOUR",ONE_MIN * 60);
define("ONE_DAY", ONE_HOUR * 24);
define("ONE_YEAR",ONE_DAY * 365);

and then do something like:

$offset = (NOW - ((ONE_HOUR * 13) + (ONCE_MIN * 4)));

in regards to actually parsing the string, you should look at a javascript function and convert to PHP, the source is available from PHP.JS:

http://phpjs.org/functions/strtotime:554

2 Comments

but this doesn't not parse the string
Updated. Updated with a possible solution.
-1

Use strtotime(), and perhaps date() later, e.g.:

$thetime = strtotime('13 hours 4 minutes ago');
$thedate = date('Y-m-d', $thetime);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.