I have some dates that are outputted by previous javascript and into a hidden text fields.
I need to convert it to another form开发者_StackOverflowat ( dd/mm/yyyy ) and am not quite sure how to do it.
The values are dynamic so im not sure how many there will be so i guess it will need to run through a loop maybe?
I need to convert this
Wed Jan 19 2011 00:00:00 GMT+1100 (EST),Thu Jan 20 2011 00:00:00 GMT+1100 (EST),Fri Jan 21 2011 00:00:00 GMT+1100 (EST),Sat Jan 22 2011 00:00:00 GMT+1100 (EST),Sun Jan 23 2011 00:00:00 GMT+1100 (EST)
to
19/01/2011,20/01/2011,21/01/2011,22/01/2011,23/01/2011
Any help would be great.
Thanks,
$str = 'Wed Jan 19 2011 00:00:00 GMT+1100 (EST),Thu Jan 20 2011 00:00:00 GMT+1100 (EST),Fri Jan 21 2011 00:00:00 GMT+1100 (EST),Sat Jan 22 2011 00:00:00 GMT+1100 (EST),Sun Jan 23 2011 00:00:00 GMT+1100 (EST)';
$dates = explode(',', $str);
$result = array();
foreach ($dates as $date) {
$result[] = date('d/m/Y', strtotime($date));
}
echo implode(',', $result);
Also, note that this code depends on the local timezone. So if your timezone isn't GMT+11
, then you can change it with date_default_timezone_set('Australia/Canberra');
for example
<?php
echo date('d/m/Y', strtotime("Wed Jan 19 2011 00:00:00 GMT+1100 (EST)"));
You can do something like this
$str='Wed Jan 19 2011 00:00:00 GMT+1100 (EST)';
$t=strtotime($str);
echo strftime('%d/%m/%Y', $t);
Note however that strtotime will use the system timezone to convert the time - I'm in the UK and this time, when coverted to GMT, would actually display '18/01/2011'. Just something to be aware of! From the manual:
Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable. See also date_default_timezone_set()
You need to explode it and then use the strtotime function.
精彩评论