Page 1 of 1

date() MySQL to PHP

Posted: Thu Jun 23, 2005 10:43 am
by tsabar
trying to format the YYYYMMDDHHMMSS variable that i pull out of my MySQL db.

can't figure out how to take this variable (let's say $row['mydate'] = "YYYYMMDDHHMMSS") and break it up with PHP into separate variables i can use.

anybody?

thx!
o

Posted: Thu Jun 23, 2005 10:49 am
by Hebbs
tsabar,

Try this function:-

Code: Select all

function timestampToUnix( $t ) {
    $YYYY = $MM = $DD = $hh = $mm = 0;
    
    $YYYY = "$t[0]$t[1]$t[2]$t[3]";
    $MM = "$t[4]$t[5]";
    $DD = "$t[6]$t[7]";
    $hh = "$t[8]$t[9]";
    $mm = "$t[10]$t[11]";
    
    return strtotime("$DD/$MM/$YYYY $hh:$mm:00");
}

Regards

Hebbs

Posted: Thu Jun 23, 2005 10:53 am
by Burrito
check the date() function.

you can do pretty much anything you want with that as far as breaking the mysql date apart.

date() function

Posted: Thu Jun 23, 2005 10:59 am
by tsabar
i got the date function, i just don't get how to have the date() function use the variable i give it ("YYYYMMDDHHMMSS") instead of today's date.

can that be done?

Posted: Thu Jun 23, 2005 11:02 am
by Burrito
sure, just use strtotime() to change the string format to a useable "time" format for php.

ex:

Code: Select all

$mydate = "2005-01-04 10:00:30";

$day = date("d",strtotime($mydate));
$year = date("Y",strtotime($mydate));
$month = date("m",strtotime($mydate));
//etc

=)

Posted: Thu Jun 23, 2005 11:22 am
by tsabar
woohoo!

man, its the little things in life...

:P

thx,
o

Posted: Thu Jun 23, 2005 7:05 pm
by timvw

Code: Select all

SELECT UNIXTIMESTAMP(datecolumn) AS unixtime FROM table
Now you recieve a unixtimestamp..