pg.
pg.

Reputation: 2531

Regex for timestamps in php

I'm trying to take timestamps in this format:

2009-11-16T14:05:22-08:00

and make turn them into something like

2009-11-16

How do I remove everything after the "T"?

Upvotes: 0

Views: 538

Answers (4)

PaulC
PaulC

Reputation: 555

Since the OP tagged it as a php and regex question, I'll give him a php and regex answer:

$result = preg_replace("/T.*/", "", $timestamp);

Upvotes: 1

Paige Ruten
Paige Ruten

Reputation: 176645

You could use explode():

list($date) = explode('T', '2009-11-16T14:05:22-08:00');

$date would now be '2009-11-16'.

Upvotes: 1

powtac
powtac

Reputation: 41040

Maybe this works:

date('Y-m-d', strtotime('2009-11-16T14:05:22-08:00'));

Upvotes: 6

Travis
Travis

Reputation: 4078

Assuming they're all in that format, the easiest way is:

$result = substr($timestamp,0,10);

Where timestamp is your starting timestamp and result is your modified timestamp.

Upvotes: 3

Related Questions