0

i am using ACF to display an internationalized date with this code :

<?php
    $dateformatstring = "l j F Y";
    $unixtimestamp = strtotime(get_field('date'));
?>

<?php echo date_i18n($dateformatstring, $unixtimestamp); ?>

but i wish i could separate each part into span for example to obtain a result like this :

<span>*day number*</span><span>*week day*</span><span>*month*</span><span>*year*</span>

but despite many attemps, i couldn't make it -_- Thanks for your help ;-)

2
  • Why not just echo '<span>'.date_i18n('..', $timestamp).'</span><span>'.date_i18n(... (you get the idea) ?
    – kero
    Nov 14 '18 at 19:36
  • yes i get it ! :-)
    – studiok7
    Nov 14 '18 at 19:44
2

thanks to @kero, i managed to do something like this :

<?php
    $dayNumber = "l";
    $weekDay = "j";
    $month = "F";
    $year = "Y";
    $unixtimestamp = strtotime(get_field('date'));
?>

<?php echo '<span>' . date_i18n($dayNumber, $unixtimestamp) . '</span>'; ?>
<?php echo '<span>' . date_i18n($weekDay, $unixtimestamp) . '</span>'; ?>
<?php echo '<span>' . date_i18n($month, $unixtimestamp) . '</span>'; ?>
<?php echo '<span>' . date_i18n($year, $unixtimestamp) . '</span>'; ?>

Not sure it is the best way to do it but it works. Thanks !

1

There's another way to do it as demonstrated here:

  • In ACF settings, use 'Custom Format' to separate the items you want to add HTML to with a comma
    • M,d
  • In your output file, set up your date output as a variable
    • $date = get_field(‘date’);
  • Next, explode the variable you created
    • $dateArray = explode(‘,’, $date)
  • Use the array you created to add HTML to each piece of your date
    • <p class="month"><?php echo $dateArray[0]; ?></p>
    • <p class="date"><?php echo $myArray[1]; ?></p>
  • Profit!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.