Forum Moderators: coopster

Message Too Old, No Replies

Changing date formats. kinda

         

Esqulax

4:10 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



Hiya,
Anyone know how to change a date format, but if its already in text....
Heres the code


$startdate = explode("-",$row['arr_date']);
$styear = $startdate[0];
$stmonth = $startdate[1];
$stday = $startdate[2];

if the date in the db was 2008-03-11

$styear would be 2008. how can i make it 08?
Im not 100% on how mktime or strtotime works, and i think it might be one of those...

coopster

4:25 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$styear = substr [php.net]($startdate[0], 2, 2);

Esqulax

4:28 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



Thats what i love about this place, always something thinking outside the box.. i had my mind fixed on dates.. not on the fact of it being a string.
Thanks Coopster

whoisgregg

6:03 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One tiny change and it can accept either '2008' or '08' as the year and still produce the same result:

$styear = substr($startdate[0], -2, 2);

coopster

7:28 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Coming from the database I figured it was a DATE column type so it would always be 4 digits. However, you are still correct and you could even shorten it to just ...
$styear = substr($startdate[0], -2);

whoisgregg

7:37 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh yes, I completely overlooked the bit about it coming from a db. :/

coopster

7:50 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was actually going to take it a step further in my initial post but did not know if the OP was using the full date elsewhere or not after extraction from the database table. If not, the date could be formatted in the SQL query itself. Something like so ...
$sql = "SELECT SUBSTRING('2008-03-06', 3) AS yymmdd"; 
$rows = mysql_query($sql);
$row = mysql_fetch_array($rows);
list [php.net]($yy, $mm, $dd) = explode('-', $row['yymmdd']);
print "$mm/$dd/$yy";
exit;