tags:

views:

19033

answers:

12

How do I get timestamp from,for example : 22-09-2008

+3  A: 

Using mktime:

list($day, $month, $year) = explode('-', '22-09-2008');
echo mktime(0, 0, 0, $month, $day, $year);
Till
That's what I've always done. Explode it on - or / or . or whatever it's separated on, then mktime.
Rich Bradshaw
+25  A: 

php's strtotime()

gives

$timestamp = strtotime('22-09-2008');
Owen
Elegant. I had forgotten about that!
Till
How is it that you have not gotten the credit for this answer yet?!
Mark Tomlin
A: 

Thanks to both!

Wizard4U
Use the comment feature to reply to people. Don't post a new answer.
epochwolf
Also, Owen's answer should be accepted as best answer to your question. Unless there's something you didn't tell us, he deserves the points! :)
Till
+13  A: 

There is also strptime which expects exactly one format:

$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_month'], $a['tm_day'], $a['tm_year']);
Armin Ronacher
it should be tm_mon instead of tm_month and tm_mday instead of tm_day
amarillion
Beware, strptime function is not available on Windows.
understack
+8  A: 

Be careful with functions like strtotime() that try to "guess" what you mean (it doesn't guess of course, the rules are here).

Indeed 22-09-2008 will be parsed as 22 September 2008, as it is the only reasonable thing.

How will 08-09-2008 be parsed? Probably 09 August 2008.

What about 2008-09-50? Some versions of PHP parse this as 20 October 2008.

So, if you are sure your input is in DD-MM-YYYY format, it's better to use the solution offered here: http://stackoverflow.com/questions/113829/date-to-timestamp-php#113871

daremon
Valid concern. I guess strtotime() relies in your locale.
Till
A: 

hi all,how can i extract date from the mysql timestamp field?

Please don't append your question to another one - this is not a forum. Just start a new question.
amarillion
A: 

echo strtotime ("now"); // if you want today

zzapper
A: 

FROM_UNIXTIME(mysql_timestamp), FROM_UNIXTIME(mysql_timestamp,format)

A: 
A: 

If you now the format use strptime, because strtotime does a guess for the format Since strptime is not implemented in Windows there is a custom function: see http://nl3.php.net/manual/en/function.strptime.php#86572

Remember that the returnvalue tm_year is from 1900! and tm_month is 0-11

After the you can use:

$a = strptime('22-09-2008', '%d-%m-%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900)
blavla
A: 

Here is how I'd do it:

function dateToTimestamp($date, $format, $timezone='Europe/Belgrade') {

//returns an array containing day start and day end timestamps

$old_timezone=date_timezone_get();
date_default_timezone_set($timezone);
$date=strptime($date,$format);
$day_start=mktime(0,0,0,++$date['tm_mon'],++$date['tm_mday'],($date['tm_year']+1900));
$day_end=$day_start+(60*60*24);
date_default_timezone_set($old_timezone);
return array('day_start'=>$day_start, 'day_end'=>$day_end);

} $timestamps=dateToTimestamp('15.02.1991.', '%d.%m.%Y.', 'Europe/London'); $day_start=$timestamps['day_start'];

This way, you let the function know what date format you are using and even specify the timezone.

Julijan Anđelić
A: 

Here is a very simple and effectve solution using split and mtime functionns

$date="30/07/2010 13:24"; //date example
list($day, $month, $year, $hour, $minute) = split('[/ :]', $date); 
//the variables should be arranged acording to your date format and so the separators
$timestamp=mktime($hour, $minute,0, $month, $day, $year);
echo date("r", $timestamp);

I worked like a charm for me.

Victor Bojica