Ciezo Posted April 18, 2011 Share Posted April 18, 2011 Hello everybody! I need some help parsing dates. I have to read some periodic data from a file, and the date format given is a string '2011-05', meaning its the fifth week of 2011. I have to transform this string to date (format dd-mm-yyyy) and then insert it into my mysql db. ¿how can I get the day and month from that date format? I've tried to use some functions like date_parse_from_format ( string $format , string $date ) without success. Any help is appreciated! Thx! Quote Link to comment https://forums.phpfreaks.com/topic/234036-parsing-and-formatting-week-dates/ Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2011 Share Posted April 18, 2011 Well as a week is a period of seven days, and a date is a single day, your looking at a serrious struggle making this run for over a year (unless the day of the week that your date reffers to is irrelivent). as it stands, you could calculate the week as a date by taking the substing after the - (in this case 05) and then multiplying it by 365.25 and dividing the result of that by 52. take that value and add it to the integer value gained by converting from, date to integer, the 1st of january for the substring before the - (in this case 2011). now take the result of that addition and convert it back to a date. ( I think you should get 5th Feb 2011). This however works within certain constants, so there is probably a better way of doing things. Quote Link to comment https://forums.phpfreaks.com/topic/234036-parsing-and-formatting-week-dates/#findComment-1202943 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2011 Share Posted April 18, 2011 In php5.2.0 or greater - <?php $your_value = '2011-05'; list($year,$week) = explode('-',$your_value); $date = new DateTime(); $date->setISODate($year,$week); echo $date->format('Y-m-d') . "<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/234036-parsing-and-formatting-week-dates/#findComment-1202945 Share on other sites More sharing options...
AbraCadaver Posted April 18, 2011 Share Posted April 18, 2011 Might be able to streamline it, but this approach will work as well: $your_value = '2011-05'; list($year, $week) = explode('-', $your_value); $week--; echo date('Y-m-d', strtotime("+$week weeks", strtotime("$year-01-01"))); Quote Link to comment https://forums.phpfreaks.com/topic/234036-parsing-and-formatting-week-dates/#findComment-1203038 Share on other sites More sharing options...
Ciezo Posted April 18, 2011 Author Share Posted April 18, 2011 Hi guys!!! I really appreciate your help, I will try your approaches but I managed to make it myself (after reading almost the whole dates php manual) 1. Get $year = 2011 2. Get $week = 05 3. Get timestamp: $time = strtotime($year . "W" . $week); //I think it returns the first day of that week, starting on Monday 4. Get date: $date = date("j-n-Y", $time); Quote Link to comment https://forums.phpfreaks.com/topic/234036-parsing-and-formatting-week-dates/#findComment-1203084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.