westman Posted January 10, 2015 Share Posted January 10, 2015 How do i get the time of 18:30 UTC + 0 ? Link to comment Share on other sites More sharing options...
Ingolme Posted January 10, 2015 Share Posted January 10, 2015 You can get a timestamp using the gmmktime() function. Otherwise, I don't know exactly what format you want the time to be in. Link to comment Share on other sites More sharing options...
westman Posted January 11, 2015 Author Share Posted January 11, 2015 I am working in JavaScript and need to past the time in a URL from php so the day will be a number from 1-7 (1 = sunday) and the time (for example 13:32) so i need the day and hour in 2 different php variables could you give an example? Link to comment Share on other sites More sharing options...
Ingolme Posted January 11, 2015 Share Posted January 11, 2015 Is the user supposed to be the one choosing these values? Is there a form involved? Link to comment Share on other sites More sharing options...
westman Posted January 11, 2015 Author Share Posted January 11, 2015 No the user is not the one who is choosing the time.It is a set UTC+0 time in the system. Link to comment Share on other sites More sharing options...
Ingolme Posted January 11, 2015 Share Posted January 11, 2015 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 comment Share on other sites More sharing options...
westman Posted January 12, 2015 Author Share Posted January 12, 2015 I am looking for "Friday" witch = 6 in JavaScript. I have a count down in JavaScript and i need php to give the day and hour to JavaScript so the count down is the same all over the world. The day "Friday (6)" The hour "18" The minute "30" Link to comment Share on other sites More sharing options...
Ingolme Posted January 12, 2015 Share Posted January 12, 2015 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 comment Share on other sites More sharing options...
westman Posted January 12, 2015 Author Share Posted January 12, 2015 (edited) in this echo i am getting "-" in the number for example "-384847000" why? 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 January 12, 2015 by westman Link to comment Share on other sites More sharing options...
Ingolme Posted January 12, 2015 Share Posted January 12, 2015 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 comment Share on other sites More sharing options...
westman Posted January 12, 2015 Author Share Posted January 12, 2015 (edited) 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 https://lh3.googleusercontent.com/-rToyVjegrJ4/VK7brlHiFgI/AAAAAAAAGFA/e9NkeE9Y5Nc/s1600/Capture.JPG 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 January 12, 2015 by westman Link to comment Share on other sites More sharing options...
justsomeguy Posted January 12, 2015 Share Posted January 12, 2015 http://php.net/manual/en/function.getdate.php Link to comment Share on other sites More sharing options...
westman Posted January 13, 2015 Author Share Posted January 13, 2015 I could not get any information from php's site Link to comment Share on other sites More sharing options...
justsomeguy Posted January 13, 2015 Share Posted January 13, 2015 What do you mean you couldn't get information? The getdate function is the way that you convert a timestamp to its various values in PHP. Link to comment Share on other sites More sharing options...
westman Posted January 16, 2015 Author Share Posted January 16, 2015 (edited) 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 January 16, 2015 by westman Link to comment Share on other sites More sharing options...
justsomeguy Posted January 16, 2015 Share Posted January 16, 2015 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 comment Share on other sites More sharing options...
westman Posted January 16, 2015 Author Share Posted January 16, 2015 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)? $The_Time_In_London_Is from this basic platform I may be able to do the maths. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 19, 2015 Share Posted January 19, 2015 You can set the default timezone to be London, then the time function will get the current timestamp. You can use the date function to format a timestamp however you want to show it. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now