March 31, 2016

How to make symfony forms support datetime-local input type?

Starx’s Question:

Most of the browsers are dropping support for datetime and also datetime-local as a valid input type. As of the time of writing this question, there are more support for support for datetime-local than datetime(which is almost non-existent).

If you building forms using Symfony’s form builder, it supports datetime but not datetime-local. So how would you make symfony form builder accept datetime-local input type and keep the rest of the functionality of the input type same?

One way this problem can be solved is, if we can change the text of input type to say datetime-local which can be done by overwriting the DateTimeType and using that.

<?php

namespace AppBundleComponentFormExtensionCoreType;

use SymfonyComponentFormFormInterface;
use SymfonyComponentFormFormView;

class DateTimeType extends SymfonyComponentFormExtensionCoreTypeDateTimeType
{
    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['widget'] = $options['widget'];

        // Change the input to a HTML5 datetime input if
        //  * the widget is set to "single_text"
        //  * the format matches the one expected by HTML5
        //  * the html5 is set to true
        if ($options['html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
            $view->vars['type'] = 'datetime-local';
        }
    }

}
September 17, 2013

Convert String in Array JQUERY

User2789569’s Question:

I have the data array return of the server :

data.arr = [[Date.UTC(2013, 9, 17),1],[Date.UTC(2013, 9, 18),5],[Date.UTC(2013, 9, 19),2],[Date.UTC(2013, 9, 20),4]]

    [0] "[Date.UTC(2013, 9, 17),1]" String
    [1] "[Date.UTC(2013, 9, 18),5]" String
    [2] "[Date.UTC(2013, 9, 19),2]" String
    [3] "[Date.UTC(2013, 9, 20),4]" String

I need to pass only value to a function that recepeit an array[i,y], i need that stay following; i need to remove the “”.

    [0] [Date.UTC(2013, 9, 17),1]
    [1] [Date.UTC(2013, 9, 18),5]
    [2] [Date.UTC(2013, 9, 19),2]
    [3] [Date.UTC(2013, 9, 20),4]

How to do it?

data.arr = [[Date.UTC(2013, 9, 17),1],[Date.UTC(2013, 9, 18),5],
[Date.UTC(2013, 9, 19),2],[Date.UTC(2013, 9, 20),4]]

This code should already give you a timestamp value, but I am assuming you are asking about when it is treated as string so as suggested in comments, you can use eval to evaluate the string as Data Object.

for(var i = 0; i < data.arr.length; i++) {
     data.arr[i] = [data.arr[i][0], eval(data.arr[i][1])];
}

Here is a demo: http://jsfiddle.net/8eLEH/

October 28, 2012

Is there a Javascript library to go from geopoint and time to timezone?

Question by Luke Hoersten

I’m looking for a Javascript library that can take a geopoint and date and return the timezone. The date is used as input in order to determine the daylight savings time status for that date and location.

I’ve seen there are services which make this information available:

The information is mostly static, though, so I’d like to not have to depend on external services and am therefore looking for a library. Here’s an example of how I’d like it to work:

tz = geodate.timezone(someFutureDate, lat, lng) => TimeZoneObject
tz.abbr() => "CST"

Answer by Starx

Finding the time zone based on the latitude and longitude has been previously asked on this community. Check

  1. Determine timezone from latitude/longitude without using web services like Geonames.org
  2. Get PHP Timezone Name from Latitude and Longitude? (Similar to 1)

Quoting an answer from Michael

  • Download the database of cities from geonames.org
  • convert it to a compact lat/lon -> timezone list
  • Use an R-Tree implementation to efficiently lookup the nearest city (or rather, its timezone) to a given coordinate

Now, answering the JavaScript part, JS alone cannot perform such queries as it runs on Client’s Machine. SO either you have to create an API to query the results or send AJAX request to a server page returning you the results.

Sending AJAX request would be a lot better and easier.

March 24, 2012

Calculate time left from specific datetime in php

Question by user1286499

may I know how do I calculate time left from specific datetime in php?

Example I want user can request a new confirmation code after 30 minute if already requested.

$request_date = '2012-03-24 13:03:17';
$please_wait  = '30 minutes';

How do I calculate the time left?

Answer by safarov

Time left for new confirmation code:

$request_date = '2012-03-24 13:03:17';
$please_wait  = '30 minutes';
$future_time = strtotime("+" . $please_wait, strtotime($request_date));
$time_left = $future_time - time(); //seconds left

Answer by Starx

Calculate the requested timestamp and sustract with currect time stamp

Solution:

$future = strtotime($request_date)+1800; // 1800 is 30 minutes
echo date("i", $future-time()); //this is the remaining minutes
...

Please fill the form - I will response as fast as I can!