0

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!

Edited by LastMitch: grammer

2
Contributors
19
Replies
21
Views
5 Years
Discussion Span
Last Post by leviathan185
Featured Replies
  • this will give you the result you want: `$todayDate = date("l, F d, Y, g:ia");` Read More

  • 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>"; ?> Read More

  • 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 Read More

0

@leviathan185

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

Edited by LastMitch: grammer

0

@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.

Edited by LastMitch: grammer

0

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.

0

@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.

Edited by LastMitch: grammer

0

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.

0

@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.

Edited by LastMitch: grammer

0

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');?

0

@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

Edited by LastMitch: grammer

0

@leviathan185

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

I'm run that code now.

0

@leviathan185

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

The timezone is in Europe/Paris

This is really weird!

1

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>";
    ?>
Votes + Comments
Thanks for the solution!
1

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

0

@leviathan185

I will try that code and test it out.

0

@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.

Edited by LastMitch: grammer

0

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

0

@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

Edited by LastMitch: grammer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.