Aureole Posted February 5, 2008 Share Posted February 5, 2008 Does Javascript have such functions and do they work similarly to PHP's? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 5, 2008 Share Posted February 5, 2008 split() is like explode() concat() is like implode() Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 I'll try it myself, but I've only just started learning Javascript today so I might get stuck, but thanks for the help so far. Alright, I need to split... (the numbers may change, but you get the idea)... 02.04.2008 7:53 PM PST into... 02.04.2008 7:53 PM PST ...then split... 02.04.2008 ...into... 02 04 2008 ...then split... 7:53 ...into... 7 53 Then I think I'll be able to work with the Javascript date/time functions after that. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 5, 2008 Share Posted February 5, 2008 <script language="javascript"> var fullstamp="02.04.2008 7:53 PM PST"; var datestamp="02.04.2008"; var hourminstamp="7:53"; var firstexplosion = fullstamp.split(" "); var secondexplosion = datestamp.split("."); var firstexplosion = hourminstamp.split(":"); </script> Then you use array keys; just like you would in PHP; too get it to display like you want it to. Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 Alright, I have this: var date = '02.04.2008 7:53 PM PST'; var splitdate = date.split(' '); var mdy = splitdate[0].split('.'); var month = mdy[0]; var day = mdy[1]; var year = mdy[2]; var hm = splitdate[1].split(':'); var hours = hm[0]; var minutes = hm[1]; var ampm = splitdate[2]; document.write("Month: " + month); document.write("Day: " + day); document.write("Year: " + year); document.write("Hours: " + hours); document.write("Minutes: " + minutes); document.write("AM/PM: " + ampm); This works, now is there anyway I can create a UNIX timestamp with Javascript? If not then I'll just have to do something else. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 Well why didn't you say that to begin with.... <script type="text/javascript"> var date = '02.04.2008 7:53 PM PST'; var d = Date.parse(date); document.write(d); </script> more date/time js functions: http://www.w3schools.com/jsref/jsref_obj_date.asp Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 That gives me NaN. ??? Basically, here's what I'm going to do: #1 Loop through all <li> that have a class of "date" #2 For each one of those, do whatever I need to (e.g. splitting) to get it into a format I can work with #3 Change the date depending on a var e.g. If the var is 0, then that means GMT. The date contained within the <li> will ALWAYS be PST. So it will do the maths to convert the PST date to GMT then set that <li>'s innerHTML to the new Date then go onto the next <li> If the var is -5, then that means EST etc. etc. I'm probably going about this whole thing the wrong way, but it doesn't matter to me... I just want to get it working. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 5, 2008 Share Posted February 5, 2008 http://www.google.com/search?hl=en&q=Javascript+Unix+Timestamp&btnG=Google+Search Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 That gives me NaN. ??? Basically, here's what I'm going to do: #1 Loop through all <li> that have a class of "date" #2 For each one of those, do whatever I need to (e.g. splitting) to get it into a format I can work with #3 Change the date depending on a var e.g. If the var is 0, then that means GMT. The date contained within the <li> will ALWAYS be PST. So it will do the maths to convert the PST date to GMT then set that <li>'s innerHTML to the new Date then go onto the next <li> If the var is -5, then that means EST etc. etc. I'm probably going about this whole thing the wrong way, but it doesn't matter to me... I just want to get it working. Sorry, that is what I get for assuming it works. It doesn't like the format your date is in. A little manipulation and this works: var date = '02.04.2008 7:53 PM PST'; var splitdate = date.split(' '); splitdate[0] = splitdate[0].replace(/\./g,'/'); var d = Date.parse(splitdate.join(' ')); document.write(d); ...and that will give you milliseconds...divide by 1000 for seconds Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 Is that there time-stamp taking into account whether it's AM or PM? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 Is that there time-stamp taking into account whether it's AM or PM? yes Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 I did what you said above, then it gave me the time-stamp "1202183580000". I then created a php file called test.php. Inside test.php I have: <?php echo( date( 'l jS M Y, g:i A', $_GET['t'] ) ); ?> I went to test.php?t=1202183580000 in my Browser and the output was: Monday 18th Jan 2038, 10:14 PM ...something's not right there. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 5, 2008 Share Posted February 5, 2008 Try this timestamp and see what it gives you: 1202111580. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 ...and that will give you milliseconds...divide by 1000 for seconds Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 It gave me: Monday 4th Feb 2008, 2:53 AM, the original was: 02.04.2008 7:53 PM PST So it's close-ish... I just realized I need to change the day and month around in the date() function in test.php... but even after changing that it'll still be off slightly. ...and that will give you milliseconds...divide by 1000 for seconds I saw that, but I didn't understand what you meant at first... Javascript... ugh. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 Ok...at least we agree the JavaScript should be giving you 1202183580000 Change your PHP code to this and let me know what it says... <?php echo( date( 'l jS M Y, g:i A T', $_GET['t'] ) ); ?> Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 I'm sorry, I've never been good with date/time stuff. It outputs: Monday 4th Feb 2008, 2:53 AM EST Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 impossible... <?php echo( date( 'l jS M Y, g:i A T', 1202183580 ) ); ?> will give you: Monday 4th Feb 2008, 10:53 PM EST check your timestamp Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 also...what timezone do you want the date printed by PHP to be in? PST also? Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 I changed the test.php file to exactly what you said and the output is now "Monday 4th Feb 2008, 10:53 PM EST" ... eh? I'm pretty darn certain that I entered the time-stamp correctly before. But anyway if you remember when we first created the time-stamp with Javascript the date was "02.04.2008 7:53 PM PST" I'm fairly certain that "02.04.2008 7:53 PM PST" is not "Monday 4th Feb 2008, 10:53 PM EST" I thought PST was GMT - 6 and EST was GMT - 5... I hate dates/times, no fun! also...what timezone do you want the date printed by PHP to be in? PST also? Well I don't actually want to do anything with PHP, I was just testing the time-stamp as I feel ok using PHP where as Javascript scares me... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 Ok...well...to finish the test, you need to tell your PHP to use PST <?php date_default_timezone_set('America/Los_Angeles'); echo( date( 'l jS M Y, g:i A T', $_GET['t'] ) ); ?> and the url to the file should look like: test.php?t=1202183580 that should prove the timestamp. also, if you ever want to test a timestamp, just go to http://www.unixtimestamp.com/ Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 and just look on the bright side...you got a crash course in timezones Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 Yeah... I guess so. I'm still miles away from accomplishing what I set out to do, but hey... it's Javascript... I expected as much. I really hate it, I don't know why. Thanks for the help any who... Does anyone have any idea how to loop through all elements that have the same class then for each one do stuff? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 I would look into getting familiar with a JavaScript library. It makes using JavaScript quite a bit easier. I think the easiest one to use is JQuery. It also has the ability to get elements by class. Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 5, 2008 Author Share Posted February 5, 2008 Now there's something I can cope with, I use jQuery... and I like it... it's just normal Javascript that I despise. So, doing what I'm trying to do would be easier with jQuery then? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.