Rabin Lama Dong
Rabin Lama Dong

Reputation: 2476

What is the best way to insert only month, day and time in database table?

I am working on php/mysqli. Here, I need to create a table field where I have to store date and time. Not the year, just month, day and time. What kind of field do I create ? Also, I will have to use the stored data in any given time to make comparisons with the user's current date and time. What could be the best method to do so ?

Upvotes: 2

Views: 217

Answers (1)

deChristo
deChristo

Reputation: 1878

You can use something like to separate the datetime:

$today = date("Y-m-d h:i:sa");
$dateTimeParts = explode(" ", $today);

//first separate date and time:
$date = $dateTimeParts[0];
$time = $dateTimeParts[1];

//separate date's year, month and day
$dateParts = explode("-",$date);


echo "Monthe and day: ".$dateParts[1]. "-" . $dateParts[2]."\n";
echo "Time: ".$time;

Then you can store the entire date value (for reference) and the parts as dateDay, dateMonth and dateTime for example. Or you can implode all the parts and store only one column:

$newDate = [];
array_push($newDate, $dateParts[1]); //month
array_push($newDate, $dateParts[2]); //year
array_push($newDate, $time);
$relevantDateParts = implode(" ", $newDate);

Upvotes: 1

Related Questions