Member Avatar for LastMitch

Hi

I'm just learning the date() function.

I'm trying create a Daylight Savings

I used $todayDate = date("D-m-d-Y-T-a"); because I am going to run it that day.

For example:

Daylight Savings is on Sunday, November 4, 2012, 2:00am.

My issue is that once I run the code my answer is this:

Daylight Savings is on Sun-11-04-2012-CEST-pm

it should be this

Daylight Savings is on Sunday, November 4, 2012, 2:00am.

So this is what I got so far:

<?php

//Daylight Savings
$todayDate = date("D-Y-m-d-T-a");
echo "Daylight Savings is on ".$todayDate."<br>";

?>

Any Suggestions and explanation will help. I appreciate it. Thanks!

this will give you the result you want: $todayDate = date("l, F d, Y, g:ia");

Member Avatar for LastMitch

@leviathan185

Thanks for the reply and example! I will test it out.

Member Avatar for LastMitch

@leviathan185

The code you provided it's a bit weird because it's 6 hours ahead of my time which is in Eastern Standard Time in America.

Right now it's 6:00 pm - Eastern Standard Time but the code you provided is 6 hours ahead it's 12:00 am? How is that possible?

I think the $todayDate should show the current time regardless of time zone.

are you using a remote host? date() will take the date and time from the server unless you specify the default time zone.

failing that you can try $todayDate = date("l, F d, Y, g:ia", time() - 60 * 60 * 6); that will take 6 hours off the current time.

Member Avatar for LastMitch

@leviathan185

Thanks for the reply back!

are you using a remote host? date() will take the date and time from the server unless you specify the default time zone.

No, I'm not remote host. My default time zone is Eastern Standard Time

$todayDate = date("l, F d, Y, g:ia", time() - 60 * 60 * 6);

that's won't work because it's Daylight Savings Time you can't minus 6 hours! If I was in a different time zone then there would be the same issue.

I hope you get the idea.

I dont understand why it is giving you a time that is 6 hours fast if your time zone is site to EST. In your original post you said the out output was Daylight Savings is on Sun-11-04-2012-CEST-pm. Pretty sure CEST that is Central European Summer Time

Change your server settings or you can use date_default_timezone_set('America/Your_City');

you can also check if it is daylight savings time using date("I") it will return 1 for yes and 0 for no. you can use it in an if statement.

Member Avatar for LastMitch

@leviathan185

I dont understand why it is giving you a time that is 6 hours fast if your time zone is site to EST. In your original post you said the out output was Daylight Savings is on Sun-11-04-2012-CEST-pm. Pretty sure CEST that is Central European Summer Time

The T is for timezone from my code: $todayDate = date("D-Y-m-d-T-a");

Change your server settings or you can use date_default_timezone_set('America/Your_City');

I'm using Easy PHP to run my code. So I don't think that is the issue.

This is very confusing and I don't know where to correct the code I mean what letter should be part of $todayDate = date("l, F d, Y, g:ia"); to correct it.

you can also check if it is daylight savings time using date("I") it will return 1 for yes and 0 for no. you can use it in an if statement.

This is my original whole code:

<?php
//Daylight Savings
$todayDate = date("D-Y-m-d-T-a");
echo "Daylight Savings is on ".$todayDate."<br>";
?>

Now I'm using the code you provided:

<?php
//Daylight Savings
$todayDate = date("l, F d, Y, g:ia");
echo "Daylight Savings is on ".$todayDate."<br>";
?>

When I echo it:

Daylight Savings is on Sunday, November 4, 2012, 2:00am.

Even though the day, date is right but the time is 6 hours ahead?

I didn't expect this bugged pop up at all. I thought this will be a simple lesson for me to learn and understand how date() function work.

Yes the T is for Timezone and your output is giving CEST for the timezone. did you try date_default_timezone_set('America/Your_City');?

Member Avatar for LastMitch

@leviathan185

Yes the T is for Timezone and your output is giving CEST for the timezone. did you try date_default_timezone_set('America/Your_City');?

I'm running it on Easy PHP and WAMPP and it didn't work

This is what it echo out

Daylight Savings is on Friday, October 19, 2012, 4:53am

but my time is

Daylight Savings is on Thursday, October 18, 2012, 10:53pm

can you please post the output of echo date("e");

Member Avatar for LastMitch

@leviathan185

can you please post the output of echo date("e");

I'm run that code now.

Member Avatar for LastMitch

@leviathan185

When I run that code: echo date("e");

The timezone is in Europe/Paris

This is really weird!

try this, remembering to change "Your_City" to the city you live in.

    <?php
    //Daylight Savings
    date_default_timezone_set('America/Your_City'); 
    $todayDate = date("l, F d, Y, g:ia");
    echo "Daylight Savings is on ".$todayDate."<br>";
    ?>
commented: Thanks for the solution! +5

further to this you will need to edit your php.ini file and change date.timezone = "Europe/Paris" to date.timezone = "America/Your_City"

This is a more suitable solution

Member Avatar for LastMitch

@leviathan185

I will try that code and test it out.

Member Avatar for LastMitch

@leviathan185

I put in my New York in Your_City

date_default_timezone_set('America/New York');

This code you provided didn't work:

<?php
//Daylight Savings
date_default_timezone_set('America/New York');
$todayDate = date("l, F d, Y, g:ia");
echo "Daylight Savings is on ".$todayDate."<br>";
?>

it still echo out:

Daylight Savings is on Friday, October 19, 2012, 5:31am

not

Daylight Savings is on Thursday, October 18, 2012, 11:31pm

I also create a php.ini and put in my www folder

I put this date.timezone = "America/New York" in the php.ini file.

your php.ini should already exist somewhere. I dont know much about easy php but on my xampp server it is located in /xampp/php/php.ini and it should be similar for you. I would imagine it would be in the same folder that contains your www folder.

Once you update php.ini you will need to reboot your server to reload the php.ini settings

Member Avatar for LastMitch

@leviathan185

I fixed the issue. I put New York instead of New_York. That's why I didn't get the results!

Thanks for being patience and helping me out with this issue. Thanks!

This is the whole code:

<?php
//Daylight Savings
date_default_timezone_set('America/New_York');
$todayDate = date("l, F d, Y, g:ia");
echo "Daylight Savings is on ".$todayDate."<br>";
?>

It echo out:

Daylight Savings is on Thursday, October 18, 2012, 11:57pm

Glad to hear lastMitch :) It will still be a good idea to permanently change it in you php.ini file though.

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.