-1

i have a table in my cakephp which have a field name datetime and datatype is datetime storing in this format

 2013-06-18 00:00:00

I need to extract the date part of the value not the time ..

i extract time like this

        $dateTime = $recentCall['Calllog']['dateTime'];
    $time = date("H:i:s",strtotime($datetime)); 

now i want to extract the date part.which i dont know how can i do this .. i have done some research but nothing works out for me

hellosheikh
  • 2,929
  • 8
  • 49
  • 115

2 Answers2

3

This isn't exactly a CakePHP answer, but a PHP one.

$dateTime  = $recentCall['Calllog']['dateTime'];
$timestamp = strtotime($dateTime);
$date      = date('Y-m-d' , $timestamp);
$time      = date('H:i:s' , $timestamp);

If you want to do somethink more Cakeish and use the CakePHP wrapper, should be:

$dateTime  = $recentCall['Calllog']['dateTime'];
$timestamp = CakeTime::fromString($dateTime);
$date      = CakeTime::format($dateTime, 'Y-m-d');
$time      = CakeTime::format($dateTime, 'H:i:s');

I hope my answer is clear. Ref: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html

Lucas Freitas
  • 194
  • 12
  • well thankyou for your answer i tried both of your solutions php and cakephp .. the php one works but Cakephp doesn't printing the date, instead of it is printing just this ..'Y-m-d' – hellosheikh Jun 29 '13 at 16:21
  • Stick to the PHP version, using `CakeTime` doesn't provide any advantages in your case. `CakeTime` is useful for Date Objects, but has been know to have some bugs (it's a smaller open-source project than PHP, so you can't expect the same level of robustness). – Costa Jun 29 '13 at 21:48
0

Use Y-m-d as first parameter in date to extract date.

$date = date("Y-m-d",strtotime($datetime)); 
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100