[thelist] Re: PHP/MySQL dates, times and autoincrements

Simon Willison simon at incutio.com
Sat Apr 27 22:47:01 CDT 2002


At 16:15 27/04/02 -0700, you wrote:

><?
>//function to convert dates of form YYYYMMDDHHMMSS to timestamp
>
>function convertToTS($a){
>
>$a = ''.$a.'';
>
>$y = $a[0].$a[1].$a[2].$a[3];
>$m = $a[4].$a[5];
>$d = $a[6].$a[7];
>$h = $a[8].$a[9];
>$n = $a[10].$a[11];
>$s = $a[12].$a[13];
>
>return mktime($h, $n, $s, $m, $d, $y);
>}
>?>

I used to use a function like this to parse dates until I discovered PHP's
strtotime() function. Basically it takes a string representation of a time
(and pretty much any string will do) and returns a unix timestamp. It uses
some kind of weird magic to do it but the results are excellent, especially
with mySQL timestamps. Check out www.php.net/strtotime for more details.




More information about the thelist mailing list