Jump to content

UTC+0 time set

Recommended Posts

Then don't use Javascript for it. Javascript dates are taken from the user's computer and can be wrong. PHP takes its dates from the server, why would you need Javascript to pass information to PHP when PHP can get the information more accurately itself?


Where is the source of this information? You said you want a day of the week and the time, which day of the week do you want?


Here's how to get a date in UTC from PHP:

// Assuming you want the day of the week and time for right this moment$now = time();$weekday = gmdate('N', $now);$time = gmdate('G:i', $now);// Do whatever you want with the $weekday and $time variables
Link to post
Share on other sites

In that case, Javascript does not need to know the date and time, it only needs to know how many milliseconds are remaining until the specified date and time. This will prevent the user's computer clock from causing problems.


You haven't specified, but I assume when you say Friday 18:30 you mean the closest Friday to now and not some random date next year. You can use PHP's strtotime() function to get dates relative to right now.


Here's how you can get the amount of time remaining until your target date and how to pass it to Javascript so that it can display a countdown:

<?php$t = strtotime('Friday 18:30');$seconds_until_t = time() - $t;?><script>var remainingTime = <?php echo $seconds_until_t * 1000; ?>;// Use the remainingTime timestamp in your countdown script////</script>
Link to post
Share on other sites

in this echo i am getting "-" in the number

for example "-384847000"




plus here i have 3848 minutes till the next Friday 18:30

Ho do I change this long number

from "-384847000"

to "day = 4 hours = 2 minutes =45"



I tried this but it did not work....


$t = strtotime('Friday 18:30');

$seconds_until_t = time() - $t;
$minutes = $seconds_until_t / 60;
$hours = $seconds_until_t / 60;
$days = $seconds_until_t / 24;
echo $seconds_until_t * 1000;
echo "<br />";
echo $minutes * 1000;
echo "<br />";
echo $hours;
echo "<br />";
echo $days;
echo "<br />";
Edited by westman
Link to post
Share on other sites

I did the subtraction in the wrong order. It should be $t - time();


But your Javascript countdown script doesn't need to be given minutes, hours and days. It should generate them based on the timestamp.

<?php$t = strtotime('Friday 18:30');$seconds_until_t = $t - time();?><script>// How long until the end of the countdown in millisecondsvar remainingTime = <?php echo $seconds_until_t * 1000; ?>;// Determine when the end of the countdown isvar targetTime = (new Date()).getTime() + remainingTime;// Execute this periodically using setTimeout or setIntervalfunction updateTimer() {    var remainingTime = targetTime - (new Date()).getTime();    // 86400000 is the number of milliseconds in a day    // We don't want the decimal part, so we floor it    var days = Math.floor(remainingTime / 86400000);    // Remove days from the amount    remainingTime %= 86400000;    // 3600000 is the number of milliseconds in an hour    var hours = Math.floor(remainingTime / 3600000);    // Remove hours from the amount    remainingTime %= 3600000;    // 60000 is the number of milliseconds in a minute    var minutes = Math.floor(remainingTime / 60000);    // Remove minutes from the amount    remainingTime %= 60000;    // 1000 milliseconds are in a second    var seconds = Math.floor(remainingTime / 1000);    // Now lets generate the time string and put it in an element    var str = days + "days " + hours + "h " + minutes + "m " + seconds + "s";    document.getElementById("countdown").innerHTML = str;}</script>
Link to post
Share on other sites

I really do appreciate your examples, they are helpful.

However, I am using App Inventor to build an app and can not use hard core code.


The variable I am using in App Inventor is:

day = 6

hour = 18

minute = 30

and the count down works





the only problem is that it takes the time from the phone.

That's why I need to use php to call the UTC+0 time.


How can I get the UTC+0 time in the format of...

day = 6 (days remaining)

hour = 18 (hours remaining)

minute = 30 (minutes remaining)

in php?

Edited by westman
Link to post
Share on other sites

Hi there,

I have been playing around with this code...

$t = strtotime('Friday 18:30');$seconds_until_t = $t - time();$t2 = strtotime('Friday 18:30');$seconds_until_t2 = $t2 - date_default_timezone_set("UTC");echo $seconds_until_t * 1000;echo "<br />";echo $seconds_until_t2 * 1000;$TimeZoneNameFrom="UTC";$TimeZoneNameTo="Europe/London";

and I have a problem with my timestampthe problem is that when I get "echo $seconds_until_t * 1000;" I see the numbers 41550000

but when I copy and past it in JavaScript

I get a long negative number and this feedback...


Do It Result: java.util.GregorianCalendar[time=384847000,areFieldsSet=true,lenient=true,zone=Europe/Istanbul,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=1970,MONTH=0,WEEK_OF_YEAR=2,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=5,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=54,SECOND=7,MILLISECOND=0,ZONE_OFFSET=7200000,DST_OFFSET=0]


Again I am trying to get a timestamp for how many days, hours and minutes are left till the next Friday 18:30 in London.


or to make thinks even easier, to get my php to print out the next Friday 18:30 in London like this "01/23/2015 18:30"


what am I doing wrong?

Edited by westman
Link to post
Share on other sites

but when I copy and past it in JavaScriptI get a long negative number and this feedback...

What does that mean? What are you copying and pasting into Javascript?What are you trying to do with this line:

$seconds_until_t2 = $t2 - date_default_timezone_set("UTC");

You're setting the default timezone, then subtracting that value from a timestamp? date_default_timezone_set returns a boolean value, you're trying to subtract true/false from a timestamp?

Do It Result: java.util.GregorianCalendar[time=384847000,areFieldsSet=true,lenient=true,zone=Europe/Istanbul,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=1970,MONTH=0,WEEK_OF_YEAR=2,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=5,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=54,SECOND=7,MILLISECOND=0,ZONE_OFFSET=7200000,DST_OFFSET=0]

I don't know what that is, but it's showing a Java object, which has nothing to do with either PHP or Javascript. I'm not sure how that fits in to anything.

Again I am trying to get a timestamp for how many days, hours and minutes are left till the next Friday 18:30 in London.

I would use getdate to get information about the current date and figure out what day it is, specifically to figure out how many days there are until the next friday. If it is friday, then I would check the time and figure out if it is before or after the target time. This will give you the number of days, if any, to add to get to the next friday at the target time. With that, you can use mktime to get the timestamp for the next friday at the target time, use time to get the current timestamp, and subtract them to get the difference between them. You can divide the timestamp by the number of seconds in a day, hour, and minute to get the elapsed time between then and now.Another way is to use the DateTime classes, which will let you use things like DateTime::diff to get the difference between 2 dates.
Link to post
Share on other sites

I know the logic but not the code.

so hopefully this question helps.


how do a make this variable hold the current time in London (in "Timestamp" format and "Date Time" format)?



from this basic platform I may be able to do the maths.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...