Viewed   110 times

i am new to php. i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.



Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.

Here's code to switch to GMT

$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));

and back to the default timezone...

$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.


// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');

// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');

// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));

// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');

// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');

There's a whole lot of functions you can use, that are much more readable that the procedural functions.

Explicit example of converting from a timezone to GMT

$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');

$date = new DateTime('2011-12-25 00:00:00', $melbourne);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
echo $date->format('Y-m-d H:i:s');

Using your Asia/Kolkata to America/New_York

$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Friday, September 23, 2022

Passing data from PHP is easy, you can generate JavaScript with it. The other way is a bit harder - you have to invoke the PHP script by a Javascript request.

An example (using traditional event registration model for simplicity):

<!-- headers etc. omitted -->
function callPHP(params) {
    var httpc = new XMLHttpRequest(); // simplified for clarity
    var url = "get_data.php";"POST", url, true); // sending as POST

    httpc.onreadystatechange = function() { //Call a function when the state changes.
        if(httpc.readyState == 4 && httpc.status == 200) { // complete and no errors
            alert(httpc.responseText); // some processing here, or whatever you want to do with the response
<a href="#" onclick="callPHP('lorem=ipsum&foo=bar')">call PHP script</a>
<!-- rest of document omitted -->

Whatever get_data.php produces, that will appear in httpc.responseText. Error handling, event registration and cross-browser XMLHttpRequest compatibility are left as simple exercises to the reader ;)

See also Mozilla's documentation for further examples

Friday, December 23, 2022

You don't have to repeat those format identifiers . For yyyy you just need to have Y, etc.

gmdate('Y-m-d h:i:s GMT', time());

In fact you don't even need to give it a default time if you want current time

gmdate('Y-m-d h:i:s GMT');  // This is fine for your purpose


You can get that list of identifiers Here

Tuesday, October 25, 2022

you need to escape the a and t as both have special meaning when used as formatting options in date()

echo date('M j at h:i a');

See it in action

Monday, October 17, 2022

use Base64.encodeToString() try this,%20int%29

File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
byte[] bytes = FileUtils.readFileToByteArray(file);

String encoded = Base64.encodeToString(bytes, 0);                                       
Utilities.log("~~~~~~~~ Encoded: ", encoded);

byte[] decoded = Base64.decode(encoded, 0);
Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));

    File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
    FileOutputStream os = new FileOutputStream(file2, true);
catch (Exception e)

public class FileUtils {

     * Instances should NOT be constructed in standard programming.
    public FileUtils() { }

     * The number of bytes in a kilobyte.
    public static final long ONE_KB = 1024;

     * The number of bytes in a megabyte.
    public static final long ONE_MB = ONE_KB * ONE_KB;

     * The number of bytes in a gigabyte.
    public static final long ONE_GB = ONE_KB * ONE_MB;

    public static String readFileToString(
            File file, String encoding) throws IOException {
        InputStream in = new;
        try {
            return IOUtils.toString(in, encoding);
        } finally {


Monday, October 31, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :