Wednesday, January 4, 2012

Masalah Coding – Adding 2 weeks from a date (PHP code)


K tetiba rini nak cerita pasai coding lak….dah berpinau campuq berserabut tau kepala aku nih memikiokan cara nak convert date nih…since last week tau…merata website aku masuk ajukan soalan…sekali dapat idea from my officemate untuk join group PHP…setelah join aku pun mulakanlah ngarang soalan…seyes benda simple tapi susah nye la hai nak dapat solutionnya….ni haaa masalh yg aku hadapi….


Tp yg bestnya sehari je aku join group nih terus aku dapat solution nye….thanks kepada yg membantu….kalau dak nek berasap lahhh aku setlekan prob yang x semnggah nih….so, sesapa yang involve ngan php programming leh la join this group to share apa2 yg x dapat disettlekan…x de lah berapsap sengsorang kan?bukan apa sharing is caring…disamping tuh leh bagi info kat orang len gak kalau wujud masalh yg sama….huhuhuhuhu

Ni group yang aku join : http://www.php.net.my/

Dan ni solution yang aku dapat from en. Mosfet83…thanks a lot…

<?php
$date = '19/11/2009';
$date = explode('/',$date);
$newdate = strtotime ( '+2 week', strtotime($date[2].'/'.$date[1].'/'.$date[0]) );
$newdate = date ('d/m/Y', $newdate);
echo $newdate;
?>

Dan ni antara contoh yang aku search untuk format date Y-m-d. Sangat mudah..hehehe..


Subtracting days from a date


The following example will subtract 3 days from 1998-08-14. The result will be 1998-08-11.
1
2
3
4
5
$date = "1998-08-14";
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
 
echo $newdate;

 

 

 

Subtracting Weeks from a date


The following example will subtract 3 weeks from 1998-08-14. The result will be 1998-07-24. Notice that the only difference in the code is the week statement.
1
2
3
4
5
$date = "1998-08-14";
$newdate = strtotime ( '-3 week' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
 
echo $newdate;

 

Subtracting Months from a date


The following example will subtract 3 months from 1998-08-14. The result will be 1998-05-14. Notice that the only difference in the code is the month statement.
1
2
3
4
5
$date = "1998-08-14";
$newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
 
echo $newdate;

 

Subtracting Years from a date


The following example will subtract 3 years from 1998-08-14. The result will be 1995-08-14. Notice that the only difference in the code is the year statement.
1
2
3
4
5
$date = "1998-08-14";
$newdate = strtotime ( '-3 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
 
echo $newdate;

 

Adding days, months, weeks and years from a date


There isn’t really much difference from subtracting and adding dates. To add dates, just use any of the examples above and replace the negative (-) with a positive (+) e.g. ‘+3 weeks’


Dan ni lagi satu cara...sama je outputnya cuma susunan code je berbeza

$date = date("Y-m-d"); //current date
$newdate = strtotime(date("Y-m-d", strtotime($date))."+1 day"); 
$newdate = strtotime(date("Y-m-d", strtotime($date))."+1 week");
$newdate = strtotime(date("Y-m-d", strtotime($date))."+2 week");  
$newdate = strtotime(date("Y-m-d", strtotime($date))."+1 month"); 
$newdate = strtotime(date("Y-m-d", strtotime($date))."+30 days");









3 comments:

  1. hahahaha...anak murid bijak nih...huhuhuuhu

    ReplyDelete
  2. wah...bagus2 dapat jugak ang solution,,,daripada aritu tak dpt2 ye.....congrate...hehehe..

    ReplyDelete

Thanks kengkawan sudi cot cet kat sini...nanti Sumesakan jejak korang balik k...