date() MySQL to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsabar
Forum Newbie
Posts: 3
Joined: Thu Jun 23, 2005 10:38 am

date() MySQL to PHP

Post 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
Hebbs
Forum Commoner
Posts: 43
Joined: Mon Apr 22, 2002 9:34 pm
Location: Perth, Western Australia

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

check the date() function.

you can do pretty much anything you want with that as far as breaking the mysql date apart.
tsabar
Forum Newbie
Posts: 3
Joined: Thu Jun 23, 2005 10:38 am

date() function

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
tsabar
Forum Newbie
Posts: 3
Joined: Thu Jun 23, 2005 10:38 am

=)

Post by tsabar »

woohoo!

man, its the little things in life...

:P

thx,
o
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

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