I need some help bad, please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
teamparadox
Forum Newbie
Posts: 1
Joined: Fri Sep 06, 2002 10:25 pm

I need some help bad, please help!

Post by teamparadox »

My website needs this new script to go live, but the following code

$date = $row["date"];
$ndate = explode("/",$date);
$ndate = mktime(0,0,0,$ndate[0],$ndate[1],$ndate[2]);
$date = strftime("%B %d, %Y",$ndate);
print $date;

Is saying the date is October 6th 2003, but the info in the database says sept 6th 2002, something is wrong with my code, im new at this and i cant figure it out, can anyone help?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe some timezone settings.
How about requesting the appropriate date format from the database?
If you're using mysql
$query = "SELECT DATE_FORMAT(myDatefield, '%M %e %y') FROM myTable";
teamparadox2k
Forum Newbie
Posts: 3
Joined: Sat Sep 07, 2002 12:14 am
Location: none
Contact:

nope

Post by teamparadox2k »

the date string the code is handling is from MySQL, its set when the user posts, i checked that and its all correct, its just somewhere in that codeitself thats wrong
User avatar
sjunghare
Forum Newbie
Posts: 16
Joined: Tue Sep 03, 2002 6:22 am
Location: Pune

Post by sjunghare »

Following check should before proceed :

1. Order of parameter in mktime(hr,min,sec,mnt,day,yr)
2. Array [$ndate] index and values
3. When inserting the some time input we provide is incorrect

When I work on the code with my database which stored the date in
Column : name - expirydate
type - datetime

Code: Select all

$date = $rowsї0];
        $ndate = explode("/",$date);

        echo $ndate&#1111;0]."<br>";  //output => Complete Date
        echo $ndate&#1111;1]."<br>";  //output => Nothing
        echo $ndate&#1111;2]."<br>"; // output => Nothing

        $ndate = mktime(0,0,0,$ndate&#1111;0],$ndate&#1111;1],$ndate&#1111;2]);
        echo "-".$ndate."<br>"; // output => -1
        $date = strftime("%B %d, %Y",$ndate);
        echo $date;//output => Nothing


I gets strange result with
1. No value in the array for index 1,2
2. mktime output give -1

regards,
Sachin
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

depends on the database's datetime format.
Using mysql you would receive a string like '2002-09-07 09:03:28' that explode("/",...) cannot separate.
teamparadox2k
Forum Newbie
Posts: 3
Joined: Sat Sep 07, 2002 12:14 am
Location: none
Contact:

humph

Post by teamparadox2k »

ok if the format the batabase is saving in is 22:26 09/06/02 then what is wrong with the code?
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

If you explode the string 22:36 11/23/2002 or whatever you would have to strip the time part first and also trim it. Why not just use your dbms to give you the correct date. It saves coding and even runs faster.

Keith
teamparadox2k
Forum Newbie
Posts: 3
Joined: Sat Sep 07, 2002 12:14 am
Location: none
Contact:

.

Post by teamparadox2k »

because i do not yet know php, my programmer has fallen off the planet and this needs to go live and i couldnt figure out the problem myself, so i turned to a php forum for help
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

What will it say if you echo "$row['date']"?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok if the format the batabase is saving in is 22:26 09/06/02 then what is wrong with the code?
you have a 2-digit-only year format for DATETIME??? That's a problem I would say.
nevertheless I think this should do

Code: Select all

preg_match('!(\d{2})/(\d{2})/(\d{2})!', $row&#1111;0], $ndate);
$ndate = mktime(0,0,0,$ndate&#1111;1],$ndate&#1111;2],$ndate&#1111;3]); 
$date = strftime("%B %d, %Y",$ndate); 
echo $date;
but still: requesting the proper format from the DB is better ;)
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Can't you do just this?

Code: Select all

&lt;?php
$ndate&#1111;3] = "20"+$ndate;
$ndate = mktime(0,0,0,$ndate&#1111;1],$ndate&#1111;2],$ndate&#1111;3]); 
$date = strftime("%B %d, %Y",$ndate); 
echo $date;
?&gt;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the mktime of (at least my version of) php can live with the 2-digit-year.
I even produces consistent output on '99'. So I won't mess with the string.
But maybe alter the pattern to '!(\d{2})/(\d{2})/(\d{2,4})!'
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Have you tried using the StrToTime() function

Takes a date string and turns it into a UNIX timestamp.
Post Reply