How do I get timestamp from,for example : 22-09-2008
Using mktime:
list($day, $month, $year) = explode('-', '22-09-2008');
echo mktime(0, 0, 0, $month, $day, $year);
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']);
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
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)
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.
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.