-1

I have a variable that contains a date

$date = "04/18/2017 04:02 PM";

This comes from a date field, This field will always contain a date like this, and What I'm trying to do is separating the date from the time

So how do i go about getting only 04/18/2017 from the variable or the time 04:02 PM ?

Thanks.

Nippledisaster
  • 278
  • 2
  • 18

1 Answers1

1

Just do a simple explode:

$explode = explode($date, ' ', 2);
$date = $explode[0];
$time = $explode[1];
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Or: `$date = "04/18/2017 04:02 PM"; print date('Y-m-d',strtotime($date));` – Peter Apr 18 '17 at 18:30
  • That requires conversion, and some overhead. Mine simply splits the string in half, and pulls the different parts. – Blue Apr 18 '17 at 18:31
  • In this case, he will come back and will ask, how can I get the name of the day:D My answer is more flexible, and I think dates should handle like date not an string and array. Your solution is requires conversion also. You convert the string to array :) – Peter Apr 18 '17 at 18:34
  • Thank you both :* I will use explode, Don't worry i got the "day" too by splitting into three pieces due to the spaces :D – Nippledisaster Apr 18 '17 at 18:52