/    Sign up×
Community /Pin to ProfileBookmark

Convert YearWeek format to date

Hello,

If I have a string in the YearWeek format, say `201950` is there a PHP function that would format this to a date, such as 2019/12/09?

I have an array of dates in the yearweek format and I want to convert them into dates.
From my research it seems that I have to go through this array and append a ‘W’ in between the year and the week number, like ‘2019W50’
and then run `strftime(‘%Y/%m/%d’, strtotime(‘2019W50’));`

Is there an easier way?

Thank you

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 24.2020 — If that strftime function does do what you want then you simply need to use the substr function to do some modification to the source value.
<i>
</i>$newdate = substr($olddate,0,4) . 'W' . substr($olddate,4,2);

Now your script has the value it needs to do what you want.
Copy linkTweet thisAlerts:
@heartstringsauthorJan 24.2020 — @ginerjm#1613578 That's perfect, thank you so much.

``<i>
</i>
$testDate = '201951';
$format = substr($s,0,4) . 'W' . substr($s, 4, 2);
echo $format . "n";

$newdate = strftime('%Y/%m/%d', strtotime($format));


echo $newdate; <i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogJan 24.2020 — Just to link up to my answer in your other thread, purely for a slight stylistic difference, I _might_ do:
<i>
</i>$testDate = '201951';
list($year, $week) = str_split($testDate, 4);
$newDate = date('Y/m/d', strtotime($year.'W'.$week));
echo $newDate;

In this case, it outputs '2019/12/16', if that's correct?
×

Success!

Help @heartstrings spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...