Biribu
Biribu

Reputation: 3823

Get part of date in php

I need to get just the day from a datetime value in php. I get from my database something like '2015-01-01 12:12:12' and I just need '2015-01-01' for adding after it ' 00:00:00' or ' 23:59:59'.

I tried:

  echo $res0['dateInit'];
  $date_day = date('yyyy-mm-dd',$res0['dateInit']);
  $date_init = $date_day . " 00:00:00";
  $date_end = $date_day . " 23:59:59";
  echo $date_init;
  echo "<br>";
  echo $date_end; 

But I get a wrong value in the second echo. What am I doing wrong?

Upvotes: 0

Views: 149

Answers (4)

JammuPapa
JammuPapa

Reputation: 148

use strtotime, check the code given below:

$date = '2015-01-01 12:12:12';
$day = date('Y-m-d',strtotime($date));
$split_date = split(" ",$date);
print "day = $day and $date and $split_date[0]";

And the output is

day = 2015-01-01 and 2015-01-01 12:12:12 and 2015-01-01

Upvotes: 3

Shoyeb
Shoyeb

Reputation: 41

echo date("l");

// Prints something like: Monday

Refer Here for mode details

Upvotes: -1

Dmitry
Dmitry

Reputation: 281

You can select only date:

SELECT *, DATE(dateInit) FROM ...

Upvotes: 1

Harshal
Harshal

Reputation: 3622

Use Strtotime :

 $date_day = date('Y-m-d',strtotime($res0['dateInit']));

Upvotes: 4

Related Questions