Suba
Suba

Reputation: 199

Date format PHP

I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables

// unix
$date = 1436713200
// string
$time = '15:00';

to get a date format 2015-07-12 15:00 but failing, using this

$newdate = date('Y-m-d H:i:s', $date.' '.$time);

I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.

Upvotes: 0

Views: 111

Answers (3)

Rex Rex
Rex Rex

Reputation: 1030

Use this

$date =  date('Y-m-d','1436713200');
// string
$time = '15:00';

echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

I would suggest you to use DateTime instance to avoid timezone issues:

$d = date_create('@1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00

Upvotes: 3

kazy
kazy

Reputation: 1141

You do not have to provie the $time variable. Unix time is a full date with time.

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

Upvotes: 0

Related Questions