In php if we provide a particulr date how to display the name of the day i.e; whether that day lies on sunday or monday...

Can you please give me idea.

Dani AI

Generated

's quick answer (using strtotime + date("l")) is fine when the goal is "what weekday was that exact date?" — and if the input omits the year (for example "March 31") most parsers will assume the current year. If the input includes a year but the intent is "same month/day in the current year", the year must be replaced before asking for the weekday.

A robust way is to parse the incoming string with an explicit format, take its month and day, set the current year, then format the weekday. This avoids ambiguous MM/DD vs DD/MM parsing and lets the code handle invalid dates (for example Feb 29). Example using DateTime:

$input = '1987-03-31';
$dt = DateTime::createFromFormat('Y-m-d', $input);
if ($dt) {
    $month = (int)$dt->format('m');
    $day   = (int)$dt->format('d');
    $currentYear = (int)(new DateTime())->format('Y');
    if (checkdate($month, $day, $currentYear)) {
        $dt->setDate($currentYear, $month, $day);
        echo $dt->format('l');
    } else {
        // handle Feb 29 on a non-leap current year
    }
}

Notes and gotchas: always parse with an explicit format (DateTime::createFromFormat) to avoid locale/format mistakes; validate with checkdate() or DateTime::getLastErrors(); decide how to handle Feb 29 if the current year is not a leap year (map to Feb 28, Mar 1, or report invalid); set a timezone explicitly (DateTimeZone or date_default_timezone_set) if time-of-day could push the date across a boundary; use IntlDateFormatter when day names must be localized.

Further reading: see the PHP docs for DateTime::createFromFormat and checkdate.

Recommended Answers

All 5 Replies

Convert that date to unixtimestamp using strtotime. Then use date("l") to get the day.
http://in.php.net/date
eg.

$date = "2009-03-31";
$timestamp = strtotime($date);
echo date("l",$timestamp);

I want to dsiplay it irrespective of year for example if i give the date that belongs to year 1987 it should display the day with respect to present year.

I want to dsiplay it irrespective of year for example if i give the date that belongs to year 1987 it should display the day with respect to present year.

Yes. That is what it does. Try out that example by giving 1987's date :)

I gave input as previous year but tried to looking for present year.
Thank you so much.

You are welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.