Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts

Saturday, January 28, 2017

DateTime Picker · Bootstrap

You can download full example from here



<head>
    <title>Bootstrap Date Time Picker Example</title>
    <script type="text/javascript" src="jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="moment-with-locales.js"></script>
    <script type="text/javascript" src="bootstrap.min.js"></script>
    <script type="text/javascript" src="bootstrap-datetimepicker.js"></script>

    <link rel="stylesheet" href="bootstrap.min.css"/>
    <link rel="stylesheet" href="font-awesome.min.css"/>
    <link rel="stylesheet" href="bootstrap-datetimepicker.css"/>

    <script type="text/javascript">
        $(function () {
            var start = new Date();
            start.setMinutes(start.getMinutes() + 20);
            var start_value = start.getFullYear() + "-" + (start.getMonth() + 1) + "-" + start.getDate();
            start_value += " " + start.getHours() + ":" + start.getMinutes();

            start.setDate(start.getDate() + 1);

            $('#datetimepicker1').datetimepicker({
                defaultDate: start_value,
                format: 'DD-MM-YYYY hh:mm A',
                disabledDates: [
                    start
                ],
                daysOfWeekDisabled: [0, 6],
                sideBySide: false,
                showTodayButton: true,
                showClear: true,
                showClose: true,
                onSelect: function () {
                    var minDate = $(this).datepicker('getDate');
                    $('#datetimepicker2').datepicker( "option", "minDate", minDate);
                }
            });

            $('#datetimepicker2').datetimepicker({
                defaultDate: start_value,
                format: 'DD-MM-YYYY hh:mm A',
                disabledDates: [
                    start
                ],
                daysOfWeekDisabled: [0, 6],
                sideBySide: false,
                showTodayButton: true,
                showClear: true,
                showClose: true
            });

            $('#datetimepicker3').datetimepicker({
                format: 'DD-MM-YYYY hh:mm A'
            });
            $('#datetimepicker4').datetimepicker({
                useCurrent: false,
                format: 'DD-MM-YYYY hh:mm A'
            });
            $("#datetimepicker3").on("dp.change", function (e) {
                $('#datetimepicker4').data("DateTimePicker").minDate(e.date);
            });
            $("#datetimepicker4").on("dp.change", function (e) {
                $('#datetimepicker3').data("DateTimePicker").maxDate(e.date);
            });
        });
    </script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class='col-sm-6'>
            <div class="form-group">
                <div class='input-group date' id='datetimepicker1'>
                    <input type='text' class="form-control" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
        </div>
        <div class='col-sm-6'>
            <div class="form-group">
                <input id="datetimepicker2" type="text" class="form-control"/>
            </div>
        </div>
    </div>
    <div class="row">
        <div class='col-md-6'>
            <div class="form-group">
                <div class='input-group date' id='datetimepicker3'>
                    <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                </div>
            </div>
        </div>
        <div class='col-md-6'>
            <div class="form-group">
                <div class='input-group date' id='datetimepicker4'>
                    <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                </div>
            </div>
        </div>
    </div>
</div>
</body>


Thursday, November 10, 2016

Php determine if date string is a valid date in that format

Code snippet

function validateDate($date_string)
{
    $date_value = DateTime::createFromFormat('Y-m-d', $date_string);
    return $date_value && $date_value->format('Y-m-d') === $date_string;
}

Some examples

var_dump( validateDate('2016-13-10'));   // false
var_dump( validateDate('20160-13-10'));  // false
var_dump( validateDate('2016-11-32'));   // false
var_dump( validateDate('2017-02-29'));   // false

var_dump( validateDate('2016-11-02'));   // true
var_dump( validateDate('1970-12-21'));   // true
var_dump( validateDate('2017-07-29'));   // true

Online fiddle link


Tuesday, November 26, 2013

First day of next month with java Time


public static Date getNextMonth() {
    Date current = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(current);
    c.add(Calendar.MONTH, 1);
    c.set(Calendar.DATE, 1);

    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c.getTime();
}

Date nextMonth = getNextMonth();
System.out.println(nextMonth);

And output as if you check it withing november month.

Sun Dec 01 00:00:00 ALMT 2013

Tuesday, April 9, 2013

How to calculate the difference between two dates using PHP

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Getting Dates From Week Numbers in PHP


Getting Dates From Week Numbers in PHP

Recently I've been building a little project that pulls data from Google Analytics and shows your web statistics in a simple form. One thing I wanted to do was show the data for a quarter, but graphing by day is too chaotic and graphing by month only gives three points, so I wanted to graph by week. This was fine but the data returned by Analytics only gives me the week numbers, and since this project is aimed at demystifying web stats for normal people, it really needs normal dates on it!

Using DateTime::setISODate

I found that the DateTime extension has a method setISODate() which accepts the year and week number, and makes a date you can then use as normal.

$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');


You can also do this the other way around; if you have a date in PHP, there's the 'W' flag for date() which will return you the week number - very handy!


http://www.lornajane.net/posts/2011/getting-dates-from-week-numbers-in-php