Jump to content

explode and implode


Aureole

Recommended Posts

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.

Link to comment
Share on other sites

 

<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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. ;)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.