Log in

View Full Version : php date



nikomou
04-03-2007, 05:29 PM
hey,

I want to find out the date 3 months ago, but in this specific format..
Ymm. Y is the year such as 6 for 2006.

So the date now would be 704

to get this, i used this code:


$dateyear = date(y);
$dateyear = str_replace("0", "", $dateyear);
$datemonth = date(m);
$date = ("$dateyear$datemonth");

Anyone got any ideas on how i could do this with the date 3 months ago?? Ive tried using mktime but unsuccessfully..

mburt
04-03-2007, 06:08 PM
<?php
$y = substr(date('Y'),3,4);
$m = substr(date('m'),1,2);
$date = date($y.($m-3).$m);
echo $date;
?>

The above expression applies to how many months you want to back-track.
Hope this helps.

Twey
04-03-2007, 06:10 PM
$ystr = date('y');
$mstr = date('n') - 3;
if($mstr < 1) {
$mstr += 12;
$ystr -= 1;
}

$tma = mktime(date('G'), date('i'), date('s'), $mstr, date('j'), $ystr);

$tstr = date('y', $tma)[1] . date('m', $tma);mburt, that's not quite how date() works. You need to take into account the changing year, whether it's a leap year, exactly how long three months is... March the 7th minus three months is one day later on a leap year than the equivalent operation at another time.

nikomou
04-03-2007, 07:25 PM
thanks twey.. got an error msg though..

Parse error: syntax error, unexpected '[' in /home/crazy4/public_html/xxxx/date.php on line 11



<?php
$ystr = date('y');
$mstr = date('n') - 3;
if($mstr < 1) {
$mstr += 12;
$ystr -= 1;
}

$tma = mktime(date('G'), date('i'), date('s'), $mstr, date('j'), $ystr);

$tstr = date('y', $tma)[1] . date('m', $tma);
$date = ("$tstr");
echo ("$date");
?>

boxxertrumps
04-03-2007, 07:33 PM
i believe the "[1]" serves no purpose, you should take it out.
Why would date be an array?

Twey
04-03-2007, 07:48 PM
i believe the "[1]" serves no purpose, you should take it out.It's vital for the formatting that the OP wanted... I forgot PHP doesn't like array syntax on anything other than variables, though. substr() can be used instead.
Why would date be an array?It's not, it's a string. Array syntax can be used to access specific characters in strings.
$ystr = date('y');
$mstr = date('n') - 3;
if($mstr < 1) {
$mstr += 12;
$ystr -= 1;
}

$tma = mktime(date('G'), date('i'), date('s'), $mstr, date('j'), $ystr);

$tstr = substr(date('y', $tma), -1, 1) . date('m', $tma);

mburt
04-03-2007, 08:04 PM
You need to take into account the changing year, whether it's a leap year, exactly how long three months is...

Hehe... Kind of forgot about that.

nikomou
04-03-2007, 08:08 PM
works great! thanks guys!