Back to contents PHP Python Ruby Choose a language:

An important part of scraping is turning string data into structured data. Two very common things this happens with are dates and times.

For more details, read the PHP Date and Time docs, Supported Date and Time Formats, and this useful blog post.

Parsing dates/times

The easiest way is to use a general purpose function that detects many common date formats, and converts them into a PHP DateTime object.

function print_date($when) { print $when->format(DATE_ISO8601) . "\n"; } $when = date_create('21 June 2010'); print_date($when); # 2010-06-21T00:00:00+0100 $when = date_create('10-Jul-1899'); print_date($when); # 1899-07-10T00:00:00+0000 $when = date_create('01/01/01'); print_date($when); # 2001-01-01T00:00:00+0000 print get_class(date_create('21 June 2010')) . "\n"; # DateTime

You can parse times as well. $when = date_create('Tue 27 Sep 2011 00:25:48 BST'); print_date($when); # 2011-09-27T00:25:48+0100

Ambiguous cases

This sometimes goes wrong. For example, is this the 2nd March (US) or 3rd February (UK)?

$when = date_create('3/2/1999 01:00'); print_date($when); # 1999-03-02T01:00:00+0000

You can fix it with an explicit format string.

$when = date_create_from_format('d/m/Y H:i', '3/2/1999 01:00'); print_date($when); # 1999-02-03T12:04:11+0000

Saving to the datestore

This is easy as pie. You just save either the PHP DateTime object, and ScraperWiki will convert it into the format SQLite needs.

$birth_datetime = date_create('1/2/1997 9pm'); $data = array( 'name' => 'stilton', 'birth_datetime' => $birth_datetime ); scraperwiki::save(array('name'), $data);

Times are saved as UTC, as SQLite doesn't parse explicit timezones.

Querying dates

From the Web API for a scraper, you can do queries based on dates. See SQLite's date/time functions for more.

select * from swdata where birth_date < '2000-01-01'