code_junkie

How to convert date to timestamp in PHP?

Nov 11th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. $timestamp = strtotime('22-09-2008');
  2.  
  3. $a = strptime('22-09-2008', '%d-%m-%Y');
  4. $timestamp = mktime(0, 0, 0, $a['tm_month'], $a['tm_day'], $a['tm_year']);
  5.  
  6. list($day, $month, $year) = explode('-', '22-09-2008');
  7. echo mktime(0, 0, 0, $month, $day, $year);
  8.  
  9. $dateTime = new DateTime('2011-05-17');
  10. echo $dateTime->format('U');
  11.  
  12. // or
  13.  
  14. $date = date_create('2011-05-17');
  15. echo date_format($date, 'U');
  16.  
  17. // or
  18.  
  19. $date = new DateTime('2011-05-17');
  20. echo $date->getTimestamp();
  21.  
  22. // or
  23.  
  24. $date = date_create('2011-05-17');
  25. echo date_timestamp_get($date);
  26.  
  27. $a = strptime('22-09-2008', '%d-%m-%Y');
  28. $timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900)
  29.  
  30. //returns an array containing day start and day end timestamps
  31.  
  32. $old_timezone=date_timezone_get();
  33. date_default_timezone_set($timezone);
  34. $date=strptime($date,$format);
  35. $day_start=mktime(0,0,0,++$date['tm_mon'],++$date['tm_mday'],($date['tm_year']+1900));
  36. $day_end=$day_start+(60*60*24);
  37. date_default_timezone_set($old_timezone);
  38. return array('day_start'=>$day_start, 'day_end'=>$day_end);
  39.  
  40. $date="30/07/2010 13:24"; //date example
  41. list($day, $month, $year, $hour, $minute) = split('[/ :]', $date);
  42. //the variables should be arranged acording to your date format and so the separators
  43. $timestamp=mktime($hour, $minute,0, $month, $day, $year);
  44. echo date("r", $timestamp);
Add Comment
Please, Sign In to add comment