Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/234036-parsing-and-formatting-week-dates/
Share on other sites

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.

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")));

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);

 

;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.