| 
	
 | 
 Posted by Hilarion on 07/20/05 21:11 
DJ Craig wrote: 
> I'm trying to convert this date: 
> 12 May1868 
> Into this: 
> 18690512 
> That's YYYYMMDD.  So first I need to convert it to a UNIX time stamp 
> using the strtotime() function, then convert it to YYYYMMDD.  This 
> works for more rescent dates: 
> $d = date('Ymd', strtotime($d)); 
> I tested that with 19 July2005, and it worked.  But the strtotime() 
> function seems to return -1 for dates before the UNIX epoch.  How can I 
> convert these dates? 
 
 
 
Something like this should work: 
 
<?php 
 
// Accepts format 'D MmmmYYYY' or 'D MmmYYYY', returns format 'YYYYMMDD'. 
// Gives wrong results if input format is wrong, eg. year is not 4 digits. 
function convert_date( $text_date ) 
{ 
  $months = array( 
    'January'   => '01', 
    'Jan'       => '01', 
    'February'  => '02', 
    'Feb'       => '02', 
    'March'     => '03', 
    'Mar'       => '03', 
    'April'     => '04', 
    'Apr'       => '04', 
    'May'       => '05', 
    'June'      => '06', 
    'Jun'       => '06', 
    'July'      => '07', 
    'Jul'       => '07', 
    'August'    => '08', 
    'Aug'       => '08', 
    'September' => '09', 
    'Sep'       => '09', 
    'October'   => '10', 
    'Oct'       => '10', 
    'November'  => '11', 
    'Nov'       => '11', 
    'December'  => '12', 
    'Dec'       => '12' 
  ); 
  $year = substr( $text_date, -4 ); 
  $text_date = explode( ' ', substr( $text_date, 0, -4 ) ); 
  $month = $months[ $text_date[ 1 ] ]; 
  $day = str_pad( $text_date[ 0 ], 2, '0', STR_PAD_LEFT ); 
  return $year . $month . $day; 
} 
 
echo convert_date( '12 May1868' ) . "\n"; // should output 18680512 
echo convert_date( '19 July2005' ) . "\n"; // should output 20050719 
echo convert_date( '1 Jan1000' ) . "\n"; // should output 10000101 
 
?> 
 
 
Hilarion
 
  
Navigation:
[Reply to this message] 
 |