Current location: Hot Scripts Forums » Programming Languages » PHP » Todays time and date + and - five days with PHP


Todays time and date + and - five days with PHP

Reply
  #1 (permalink)  
Old 01-20-07, 05:16 PM
Oskare100 Oskare100 is offline
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
Reply With Quote
  #2 (permalink)  
Old 01-20-07, 05:27 PM
grafman grafman is offline
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.

PHP Code:



<?php
$x 
date('d');
echo 
$x '<br>';
$y date('d') + 5;
echo 
$y '<br>';
$z date('d') - 5;
echo 
$z;
?>
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."
Reply With Quote
  #3 (permalink)  
Old 01-21-07, 02:05 AM
job0107's Avatar
job0107 job0107 is offline
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_calculate04);
$year1 $year2 $year;
$month substr($date_to_calculate52);
$month1 $month2 $month;
$day_minus $day_plus substr($date_to_calculate82);
$day_minus -= 5;
$day_plus += 5;
$remainder substr($date_to_calculate1112);
$days_in_month date(tmktime(000$month0$year));
if(
$day_minus 1)
{
 
$month1--;
 If(
$month1 1){$month1 += 12;$year1--;$offset 1;}else{$offset = -1;}
 
$days_in_month1 date(tmktime(000$month10$year1));
 
$day_minus +=  $days_in_month1 $offset;
 }
If(
$day_plus $days_in_month)
{
 
$month2++;
 if(
$month2 12){$month2 -= 12;$year2++;}
 
$days_in_month2 date(tmktime(000$month20$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
Reply With Quote
  #4 (permalink)  
Old 01-21-07, 11:08 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
A lot of code for something like that. Give this a try:
PHP Code:




$date 
'2007-01-17T18:28:52.799Z';

$timestamp strtotime($date);

$time_from date('Y-m-d\TH:i:s.799\Z'$timestamp 3600 24 5);
$time_to   date('Y-m-d\TH:i:s.799\Z'$timestamp 3600 24 5); 
Reply With Quote
  #5 (permalink)  
Old 01-22-07, 05:46 PM
yonta yonta is offline
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')+5date("Y")));


$minus5 date("Y-m-d"mktime(date('h'), date('i'), date('s'), date('m'), date('d')-5date("Y"))); 
Just put in the correct format you need (replace "Y-m-d" ) and it should do the trick.

Good luck
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


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