samirah
samirah

Reputation: 199

php date conversion

How would i convert a format like:

13 hours and 4 mins ago

Into something i can use in php to further convert?

Upvotes: 0

Views: 138

Answers (5)

RobertPitt
RobertPitt

Reputation: 57258

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

Upvotes: 0

Tash Pemhiwa
Tash Pemhiwa

Reputation: 7685

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

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

Upvotes: -1

Chris Nash
Chris Nash

Reputation: 3051

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

Upvotes: 0

prodigitalson
prodigitalson

Reputation: 60413

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

Upvotes: 0

Related Questions