Current location: Hot Scripts Forums » Programming Languages » PHP » splitting up the date


splitting up the date

Reply
  #1 (permalink)  
Old 05-19-06, 07:24 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
splitting up the date

hello everyone, i wud appreciate it if someone tellme how to split a date. the date is in the format of yyyy-mm-dd(eg, 2006-04-01). can i store the year , month , date in different variables ? i want to split this date to year, month and date and store it in different variables., like $year = 2006, $month=04 and date=01. is that possible ?
thanks a lot in advance..
Reply With Quote
  #2 (permalink)  
Old 05-19-06, 07:38 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
This shoud do the trick.

PHP Code:

list($year$month$day) = explode("-"$yourdate); 

Reply With Quote
  #3 (permalink)  
Old 05-19-06, 07:39 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by n3wb!e
hello everyone, i wud appreciate it if someone tellme how to split a date. the date is in the format of yyyy-mm-dd(eg, 2006-04-01). can i store the year , month , date in different variables ? i want to split this date to year, month and date and store it in different variables., like $year = 2006, $month=04 and date=01. is that possible ?
thanks a lot in advance..
PHP Code:

$date explode("-"$date);


// yyyy-mm-dd 
$date[0// "2006" - Y
$date[1// "04" - M
$date[2// "01"  -D
// Rename thoughs vars as you see fit. 
EDIT: LOL, beat me by like 30 seconds, but his is better, with the list() command

Last edited by Christian; 05-19-06 at 12:58 PM. Reason: Merged :)
Reply With Quote
  #4 (permalink)  
Old 05-19-06, 07:48 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
Thumbs up

thanks a lotttttttttttttt.. wow! this was quick ! thanks again amigos.. have a great day.
Reply With Quote
  #5 (permalink)  
Old 05-19-06, 07:59 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
oops.. i wud like to ask you guys one more question. the scenario is : a user has applied for a job on 2006-03-19..(yyyy-mm-dd).. i have a hyperlink on the page which when clicked, should subtract the applied date(2006-02-01) from current date(eg,2006-05-19) and should show the user when he applied for the job.. (in days, as in , 62 days).. is it possible ?
yup, thanks in advance
Reply With Quote
  #6 (permalink)  
Old 05-19-06, 08:03 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
i mean, i hv no clue how to subtract 2 dates and find out the difference between their dates. <?
$dateDiff = mktime(0,0,0,04,20,2006) - mktime(0,0,0,04,10,2006);
echo '<br />Months Difference = '. floor($dateDiff/60/60/24/7/4);
echo '<br />Days Difference = '. floor($dateDiff/60/60/24);
?>
is working but not as i have stated above.. plz help !
Reply With Quote
  #7 (permalink)  
Old 05-19-06, 08:13 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You could use the strtotime() function and the function that posted Keith in your other thread

http://www.programmingtalk.com/showthread.php?t=28935

This converts the dates to timestamp.
PHP Code:

$date1 strtotime('2006-03-19');

$date2 strtotime('2006-02-01'); 
And this calculates the difference.
PHP Code:

echo CalcDiff($date1$date2);

// Outputs: 6 weeks, 4 days 
Reply With Quote
The Following User Says Thank You to Nico For This Useful Post:
n3wb!e (11-30-09)
  #8 (permalink)  
Old 05-19-06, 10:24 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts

Last edited by Keith; 05-19-06 at 10:27 AM.
Reply With Quote
  #9 (permalink)  
Old 05-19-06, 05:25 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Oops! Totally missed the link you gave... jeez.
Reply With Quote
  #10 (permalink)  
Old 05-19-06, 06:43 PM
astrkalj's Avatar
astrkalj astrkalj is offline
Newbie Coder
 
Join Date: May 2006
Location: Saint Louis, Missouri
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by nico_swd
This shoud do the trick.

PHP Code:

list($year$month$day) = explode("-"$yourdate); 

And that would be the best method in my opinion.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie found a solution to a date problem. mickey_kamer Perl 4 05-09-07 05:54 AM
js date franches JavaScript 1 11-25-04 08:42 AM
search form with date search newbieasp24 ASP 1 10-17-04 02:52 PM
Date andyX PHP 2 09-20-04 01:37 PM
Problem with date and form djavet PHP 3 05-02-04 12:37 AM


All times are GMT -5. The time now is 07:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.