Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove time stamp from string [duplicate]

Tags:

date

php

can anyone get me a code for hiding the time stamp from a string. I used this code to get the date from the string suppose the

$date_string = "02/06/2011 11:00 am - 2:00 pm";

$date = strtotime($date_string);
$date = date('m/d/y', $date);

But the out put I am getting is something like this

 1/1/70

Please suggest a better way that I could implement for this to work I want it to show like

02/06/2011
like image 612
Rahul TS Avatar asked Dec 01 '25 17:12

Rahul TS


2 Answers

If the date you're looking for is already in the string and all you want to do is remove the time range, you don't need any date manipulation. Just remove everything after the space (assuming the format of the date_string remains consistent).

$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date = explode(" ",$date_string);
echo $date[0];

Or even simpler (but untested)

echo strtok($date_string," ");  //http://codepad.org/Or1mpYOp

PHP.NET:strtok

PHP.NET:explode

like image 162
Brendan Bullen Avatar answered Dec 03 '25 06:12

Brendan Bullen


$date = strtotime($date_string);
$date = getdate($date);
$date = $date['mon'] . '/' . $date['mday'] . '/' . $date['year']
like image 42
Baruch Avatar answered Dec 03 '25 07:12

Baruch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!