Web Global Net Web Application & Web Development Project Center

Technical Issues => Javascripting => Topic started by: admin on December 23, 2016, 02:19: PM



Title: Display local Time
Post by: admin on December 23, 2016, 02:19: PM
Found this JS that appears to work in displaying the correct local time on a page - used it on a php reporting page only tested it for EST because php can only use server time which in our case was always 5 hours off of GMT. Apparently when printing this will print the time out twice - not sure why or how to fix it yet.

<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10){
  minutes = "0" + minutes;
}
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}

document.write("" + hours + ":" + minutes + " " + suffix + "");
</script>


Use this

<?php echo date('F') ?> <?php echo date('d') ?> <?php echo date('Y') ?> -  
             <script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10){
  minutes = "0" + minutes;
}
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}

document.write("" + hours + ":" + minutes + " " + suffix + "");
</script>

To produce this

December 23 2016 - 9:31 AM