Todays time and date + and - five days with PHP
01-20-07, 05:16 PM
Newbie Coder
Join Date: Apr 2005
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Todays time and date + and - five days with PHP
Hello,
When the scripts requests from the Ebay API it need to supply the current time and date plus five days and minus five days.
It needs to be in this format: 2007-01-17T18:28:52.799Z
So if now is 2007-01-20 18:28:52 then I need he script to create two variables with the following values;
(Minus five days) $time_from = 2007-01-15T18:28:52.799Z
(Plus five days) $time_to: = 2007-01-25T18:28:52.799Z
How can I make PHP add minus five days to, for example, $time_from and plus five days to, for example, $time_to as above?
Thanks in advance,
/Oskar R
01-20-07, 05:27 PM
Coding Addict
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
This is clearly illustrated in the date function in the php manual.
All you have to do is use the format specifiers to get the output format you want then add and subtract from it just like you would any normal math expression.
__________________
"Things are difficult only while you don't understand them."
01-21-07, 02:05 AM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Add 5 days & Subtract 5 days
This should do what you want.
PHP Code:
<?php
$date_to_calculate = "2007-01-20 18:28:52.799" ;
$year = substr ( $date_to_calculate , 0 , 4 );
$year1 = $year2 = $year ;
$month = substr ( $date_to_calculate , 5 , 2 );
$month1 = $month2 = $month ;
$day_minus = $day_plus = substr ( $date_to_calculate , 8 , 2 );
$day_minus -= 5 ;
$day_plus += 5 ;
$remainder = substr ( $date_to_calculate , 11 , 12 );
$days_in_month = date ( t , mktime ( 0 , 0 , 0 , $month , 0 , $year ));
if( $day_minus < 1 )
{
$month1 --;
If( $month1 < 1 ){ $month1 += 12 ; $year1 --; $offset = 1 ;}else{ $offset = - 1 ;}
$days_in_month1 = date ( t , mktime ( 0 , 0 , 0 , $month1 , 0 , $year1 ));
$day_minus += $days_in_month1 + $offset ;
}
If( $day_plus > $days_in_month )
{
$month2 ++;
if( $month2 > 12 ){ $month2 -= 12 ; $year2 ++;}
$days_in_month2 = date ( t , mktime ( 0 , 0 , 0 , $month2 , 0 , $year2 ));
$day_plus -= $days_in_month2 ;
}
if( strlen ( $month1 ) < 2 ){ $month1 = "0" . $month1 ;}
if( strlen ( $day_minus ) < 2 ){ $day_minus = "0" . $day_minus ;}
if( strlen ( $month2 ) < 2 ){ $month2 = "0" . $month2 ;}
if( strlen ( $day_plus ) < 2 ){ $day_plus = "0" . $day_plus ;}
$time_from = ( $year1 . "-" . $month1 . "-" . $day_minus . "T" . $remainder . "Z" );
$time_to = ( $year2 . "-" . $month2 . "-" . $day_plus . "T" . $remainder . "Z" );
echo( $time_from . "<br>" . $time_to );
?>
__________________
Jerry Broughton
01-21-07, 11:08 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
A lot of code for something like that. Give this a try:
01-22-07, 05:46 PM
New Member
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How about something like this
PHP Code:
$plus5 = date ( "Y-m-d" , mktime ( date ( 'h' ), date ( 'i' ), date ( 's' ), date ( 'm' ), date ( 'd' )+ 5 , date ( "Y" )));
$minus5 = date ( "Y-m-d" , mktime ( date ( 'h' ), date ( 'i' ), date ( 's' ), date ( 'm' ), date ( 'd' )- 5 , date ( "Y" )));
Just put in the correct format you need (replace "Y-m-d" ) and it should do the trick.
Good luck
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off