A Clockwork Orange
A Clockwork Orange

Reputation: 24783

PHP - Formatting multible variables into a mysql DATETIME format

My script receives multiple variables from the user. Each variable has part of the date/time (year,month,day,hour,min). All the info is stored in separate variables.

I would like to format all that into an acceptable MySQL DATETIME format so it can be saved in a DB.

Upvotes: 0

Views: 355

Answers (2)

Robert
Robert

Reputation: 11

Perhaps you want

function getDBTime($year, $month, $day $hours, $mins, $secs = "0"){
    $timestamp = mktime($hours, $mins, $secs, $month, $day, $year); 
    return date('Y-m-d H:i:s',$timestamp); 
}

and to use $var = getDBTime({your data here});

Upvotes: 1

dtbarne
dtbarne

Reputation: 8200

The mktime() and strtotime() functions are helpful here. Without an example of what you're working with, I can't give you a better example than the links.

Then you'd format your timestamp with date():

$mysql_date = date('Y-m-d H:i:s', $timestamp);

Upvotes: 3

Related Questions