Current location: Hot Scripts Forums » Programming Languages » PHP » question about date


question about date

Reply
  #1 (permalink)  
Old 10-31-06, 07:58 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
question about date

I have the following php code

PHP Code:

date('d-m-Y'
This code show the current date in day-month-year format.
What I need to do to add +1 month to current date ?

I want to show a line with current date and another line with current date + 1 month.

An example:

- 31-10-2006
- 31-11-2006

Thanks !
Reply With Quote
  #2 (permalink)  
Old 10-31-06, 08:00 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
PHP Code:

 date('d-m-Y'strtotime('+1 Month')); 

Reply With Quote
  #3 (permalink)  
Old 10-31-06, 12:09 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
I have two text input field. Each input field hold a date. By default the first field hold every time the current date, and second field hold te current date + 1 month.

Code:
Date1  <input type="text" value="31-10-2006" name="textfield" />
Date2  <input type="text" value="31-11-2006" name="textfield" />
If someone change the dates on page (if default value is not good), I need to check if second date is not bigger than +2 months..

I need to check this thing after form submited. Any idea how to do this?
Thanks

Last edited by zoliky; 10-31-06 at 12:12 PM.
Reply With Quote
  #4 (permalink)  
Old 10-31-06, 01:46 PM
odi's Avatar
odi odi is offline
Newbie Coder
 
Join Date: Oct 2006
Location: Switzerland
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
this could help you, it's not that clean, because it defines a month as 31 days, but maybe that's enough for you:

PHP Code:

function diff_in_range($date1,$date2,$range=1,$seperator="-") {

    
//split parts of the date. IMPORTANT: dd mm yyyy order (seperator is an argument)
    
$date1 explode($seperator,$date1);
    
$date2 explode($seperator,$date2);
    
    
//create UNIX-Time (seconds since the epoche)
    
$unix_date1 mktime(0,0,0,$date1[1],$date1[0],$date1[2]);
    
$unix_date2 mktime(0,0,0,$date2[1],$date2[0],$date2[2]);
    
    
//caluclate the diff
    
$unix_diff 0;
    if (
$unix_date1 $unix_date2) {
        
$unix_diff $unix_date1 $unix_date2;
    } else {
        
$unix_diff $unix_date2 $unix_date1;
    }
    
    
//If it's not too important, let's define a month as 31 days (60*60*24*31 = 2595600), this is not always true (e.g. february)
    
if ($unix_diff < (2595600*$range)) {
        return 
true;
    } else {
        return 
false;
    }
}
$date1 "31-11-2006";
$date2 "30-12-2006";

//check if there are more than 2 month (i.e. 62 days) between the dates
$result diff_in_range($date1,$date2,2); 
Reply With Quote
  #5 (permalink)  
Old 10-31-06, 02:22 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
For me is enough if PHP check the interval between date1 and date 2 if not bigger than 60 days.
Reply With Quote
  #6 (permalink)  
Old 11-02-06, 12:37 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
I receive two date in following format
PHP Code:

$_POST['pubdate'// day-month-year   (02-11-2006)

$_POST['expdate'// day-month-year   (01-01-2007) 
I need to get the difference between two date in days. Now I need to get 60 days.

When i put this :

PHP Code:

$_POST['pubdate'// day-month-year   (02-11-2006)

$_POST['expdate'// day-month-year   (02-12-2006) 
I need to get only 30 days.

I try to convert my date to unixtime format and use unixToJD function but no luck.. Anyone help me a bit ? I need to solve this problem today.

Thanks !
Reply With Quote
  #7 (permalink)  
Old 11-02-06, 02:11 AM
odi's Avatar
odi odi is offline
Newbie Coder
 
Join Date: Oct 2006
Location: Switzerland
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
I don't understand this 30-60 days issue, but to get the difference in days between two dates, you can use a modified version of my function above:

PHP Code:

function diff_days($date1,$date2,$seperator="-") {

    
//split parts of the date. IMPORTANT: dd mm yyyy order (seperator is an argument)
    
$date1 explode($seperator,$date1);
    
$date2 explode($seperator,$date2);
    
    
//create UNIX-Time (seconds since the epoche)
    
$unix_date1 mktime(0,0,0,$date1[1],$date1[0],$date1[2]);
    
$unix_date2 mktime(0,0,0,$date2[1],$date2[0],$date2[2]);
    
    
//caluclate the diff
    
$unix_diff 0;
    if (
$unix_date1 $unix_date2) {
        
$unix_diff $unix_date1 $unix_date2;
    } else {
        
$unix_diff $unix_date2 $unix_date1;
    }
    
//one day has 86400 seconds
    
return $unix_diff 86400;
}
$date1 "29-11-2006";
$date2 "01-12-2006";

//this prints "2"
echo diff_days($date1,$date2); 
with the result of this function you can check if the value is >30 or <60 or whatever you want.
Reply With Quote
  #8 (permalink)  
Old 11-02-06, 03:40 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
If date2 is smaller than date1 I need to show an error. Your function know to do this ?
I want to use this feature to publish an article, and user need to specify the start date (date1) when article appear and end date (date2) when article expire.

Thanks
Reply With Quote
  #9 (permalink)  
Old 11-02-06, 05:25 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
I have the following date :

02-11-2006

in $pubdate variable.
What I need to do to convert this date to english format : 2006-11-02 ?
Reply With Quote
  #10 (permalink)  
Old 11-02-06, 05:36 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
Try this
PHP Code:

$pubdate '02-11-2006';


$newdate implode('-'array_reverse(explode('-'$pubdate))); 
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
date format mysql and php Deansatch PHP 9 03-28-08 04:06 AM
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 02:04 PM
Quick PHP date question alej469 PHP 3 07-17-06 10:35 AM
Problem with date and form djavet PHP 3 05-02-04 12:37 AM
date question darkcarnival PHP 1 10-22-03 08:12 PM


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