Link to home
Start Free TrialLog in
Avatar of rtod2
rtod2Flag for United States of America

asked on

form a current time in my time zone

How do I form the current time on a web page that shows only my central time zone?  That is Mexico Time at www.timezonecheck.com.  I want to be able to write a <generic sentence> before some short php or html code that shows my central time in 12hr clock mode, and then complete the sentence around it.  Just a small snip-it of code should do it.  Assistance is greatly appreciated.
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Try this:

<?php
setlocale(LC_TIME, 'es_MX');
echo date("G:i");
?>

Cheers
But keep in mind that if your web server is in Mexico, you don't need to call setlocale function: you can only use

echo date("G:i");

or

echo strftime("%I:%M");

Look at http://it.php.net/manual/en/function.setlocale.php for more info
I solved using DateTimeZone built in class.

<?php
$timezone = new DateTimeZone( "America/Mexico_City");
$date = new DateTime();
$date->setTimezone( $timezone );
echo  $date->format( 'h:i a');
?>

Open in new window


For more info look at:
 - http://it.php.net/manual/en/class.datetimezone.php#datetimezone.constants.per-country
 - http://it.php.net/manual/en/timezones.php

For format strings look at http://it.php.net/manual/en/function.date.php 

Cheers
Are you 100% satisfied with only using one time zone for all requests?  In particular, are you concerned about the timezone of the CLIENT machine or the SERVER?

If all you need is the server time, you can use date() to format a string with something like 'g:ia' for the format string.
http://php.net/manual/en/function.date.php

If you know where your client is located, or you want to change the server time, you can use date_default_timezone_set() before calling date().
http://php.net/manual/en/function.date-default-timezone-set.php

If you need the time on the client machine, you can get that via JavaScript.  A tested and working example is available in the article here:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Avatar of rtod2

ASKER

MarqusG,
Thank you for this code
<?php
$timezone = new DateTimeZone( "America/Mexico_City");
$date = new DateTime();
$date->setTimezone( $timezone );
echo  $date->format( 'h:i a');
?>

Open in new window

That works perfect here http://onodot.com/xtro using a Wordpress plug-in called  PHP Widget

How could I put a sentence around the text and add a shortened 'day' before the time (i.e. Sun)?

Thank you very much for that.
Refer to this man page for the guidance about how to get the day.  Try something like the code snippet.
http://us.php.net/manual/en/function.date.php

If you want to learn more about how PHP works, this is a good book:
http://www.sitepoint.com/books/phpmysql4/

Regards, ~Ray
echo "At the moment, it is " . date('D g:ia');

Open in new window

Avatar of rtod2

ASKER

Ray, thank you
Your response indicates something different than the Central Time Zone.   Since Central is the only time I am concerned about, I am using most of the code from MarqusG, but I like your approach with the echo.  I think I can use that.  Perhaps something like.
echo "It is precisely" . <?php
$timezone = new DateTimeZone( "America/Mexico_City");
$date = new DateTime();
$date->setTimezone( $timezone );
echo  $date->format( 'h:i a');
?>
echo "right now, in my time zone."

Open in new window


Do I have that right?
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Afterthought.  From the dialog here about PHP, it seems to me that you might like this book:
http://www.sitepoint.com/books/phpmysql4/

It is very readable, as tech books go, and it has great examples, plus an online code library that you can download from.  Now in its fourth printing, it has been a part of my professional library since Version One.  More than worth the money!
Avatar of rtod2

ASKER

Thanks Guys,
Marqus, For this purpose I like your example better.  Finding it difficult to test quickly.  Is this a good tester http://writecodeonline.com/php/

When I tried your code there, I couldn't get it to work.
Avatar of rtod2

ASKER

Thank you both.  Got it...  Please see redirect >> https://www.experts-exchange.com/questions/26711826/select-your-time-zone.html