KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Using DateTimePicker #1601

Open Richb201 opened this topic on on Aug 28, 2020 - 3 comments

Richb201 commented on Aug 28, 2020

I am using DateTimePicker to have the user enter a date. The way dates work in my industry is YYYYMM. This should represent the last day of the month. For example 201307 should equal 2013-07-31 which was the last day of July. But the code below shows 2013-07-01, the first day of the month.

<form method="post">
    <?php
    DateTimePicker::create(array(
        "name" => "datePicker",
        "maxDate" => "@endDatePicker",
        "format" => "YYYYMM",
        "themeBase" => "bs4",
    ));
    ?>
    <button type="submit">Submit</button>
</form>


<?php
if (!empty($_POST["datePicker"])) {
    $timestamp= strtotime($_POST["datePicker"]);
    echo gmdate("Y-m-d", $timestamp);
}
?>

Is there some way I can cause DatePicker to use the last day of the month?

David Winterburn commented on Aug 31, 2020

Hi Richard,

When you set the DateTimePicker's format to "YYYYMM" its value only represents year and month, says "202007", nothing more, nothing less. If you want to catch the last day of that input month please try this code:

if (!empty($_POST["datePicker"])) {
    $timestamp= DateTime::createFromFormat('Ym', $_POST["datePicker"]); // Y represents year 2020, m represents month 07
    echo  $timestamp->format("Y-m-t", $timestamp); // t represents that last day of a timestamp's month
}

Let us know if this works for you. Thanks!

Richb201 commented on Aug 31, 2020

That is the issue. My grid can only get a date as YYYYMMDD. But my application (and the tax industry) uses 'last day of the month' so when a user enters 202008 my app needs to consider it as 20200830. When I use strtotime the date gets changed( auomatically) to the first day of Sept., 20200801. Suggestions?

David Winterburn commented on Aug 31, 2020

Please check my previous post's code. Thanks!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None