Home » Php » list every date with date name in php or javascript

list every date with date name in php or javascript

Posted by: admin February 25, 2020 Leave a comment

Questions:

is there a way to list every date and date name in php or javascript?
Example

  1. jan 1 Wednesday
  2. jan 2 Thursday
  3. jan 3 Friday

etc etc, if someone can spread some light on if and how to do this i would appreciate it. thank you.

How to&Answers:

You can do this with DatePeriod.

$start = new DateTime('1/1'); // first day of the current year
$interval = new DateInterval('P1D'); // iterate 1 day at a time
$end = new DateTime('1/1 next year'); // stop when we get to first day of next year
// All of these periods are equivalent.
$period = new DatePeriod($start, $interval, $end);

// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
foreach ($period as $date) {
    echo $date->format('Y-m-d')."\n";
}

This will give you every day of the current year.

Answer:

PHP Code:

<?php
$date = "2019-12-31";

do {
    $date = date("M d l", strtotime($date . "+ 1 day"));
    echo $date . "<br>" ;
}while(strtotime($date) != strtotime("2020-12-31"));

Output:

Jan 01 Wednesday
Jan 02 Thursday
Jan 03 Friday
Jan 04 Saturday
.
.
.
Dec 31 Thursday

This code starts at Jan 1st and prints out all dates in the format of Month Day Weekday and iterates through the whole year until December 31st.