Don P
Don P

Reputation: 63627

Create a mysql date from month and year

If I have a value like so:

month: 5
year: 2002

How can I use PHP to get this as a MySQL datestamp?

If I use the standard date() function $datetime = date('Y-m-d H:i:s'), I need to add the other fields.

Upvotes: 0

Views: 2261

Answers (3)

Neil Yoga Crypto
Neil Yoga Crypto

Reputation: 1035

If you want a timestamp:

<?php echo strtotime(date('2002-5-1')); ?>

If you want a datetime:

<?php echo date("Y-m-d H:i:s",strtotime('2002-5-1'));?>

Although you just can use

<?php $datetime = "2002-5-1 00:00:00";?>

Upvotes: 1

Skaparate
Skaparate

Reputation: 489

Create a day with value of 1:

$mysqlDate = sprintf("%d-%d-%d", $year, $month, 1);

Does it work for you?

Upvotes: 0

John Conde
John Conde

Reputation: 219804

I'm not entirely sure what you;re asking but I think this is what you're looking for.

$dt = DateTime::createFromFormat('Y-n', '2002-5');
echo $dt->format('Y-m-d H:i:s');

Upvotes: 4

Related Questions