0

I'm a bit of a PHP newbie, but I'm trying to assign values (dates of the current week) to a smarty array through a for loop. Unfortunately, after an hour of searching, I'm unable to figure out how to pull this off.

The best I've been able to do is assign those date values to seven individual variables (as opposed to one array housing seven values). My code for that is below. Would anyone be able to assist me with having those values loaded into an array instead of individual variables? Thanks in advance for any help.

global $smarty;

// Set current date and parse about any English textual datetime description into a Unix timestamp
$ts = strtotime('now');

// Calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) $offset = 6;

// Calculate timestamp for the Monday
$ts = $ts - $offset*86400;

// This is where I want to assign dates to a smarty array, not individual variables
for ($i=0; $i<7; $i++, $ts+=86400){
    $smarty->assign('day'.$i,date("m/d/Y l", $ts));
}

1 Answer 1

2

Why dont you make the array like:

$days = array();
for ($i=0; $i<7; $i++, $ts+=86400){
    $days[] = date("m/d/Y l", $ts);
}
$smarty->assign('days' , $days);

Then in smarty you can use the foreach loop to display the array.

smarty would go something like:

{foreach from=$days key="ind" item="day"}
  {$day}<br />
{/foreach}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply Sabeen! I'll give that a try tonight.
Yup, worked like a charm. Thanks for the help with the syntax!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.