date() and/or strtotime() not working [solved]

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
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

date() and/or strtotime() not working [solved]

Post by imstupid »

hello everybody:
Here's the situation. I've created this trouble-ticket posting system, where a user submits a problem to our glorious hr department, and while doing so, a "time posted" variable is submitted using this code:

Code: Select all

<?PHP
$ue_today = mktime(date("G"), date("i"), date("s"), date("m"), date("d"), date("Y"));
$timeposted = date ("Y-m-d G:i:s", $ue_today);	
?>
into a datetime field. I use the variable $timeposted as a unique identifier, so our hr guy and update each ticket from "unanswered" to "in progress" to "resolved" blah blah. I'm sure you get the idea. Anyway, the problem is, the update query isn't working, and I think it has to do with the way I'm handling the date or strtotime part. The date (which I modified to make it a little more readable) originally comes over in this format: Jan 18, 4:13:34 pm to this php code:

Code: Select all

<?PHP
$timeposted = $_POST['timeposted'] ;

//code here, etc..

$timeposted = date ("Y-m-d G:i:s",  strtotime($timeposted));	

// update query here
?>
This has got to be wrong somewhere, because it echos -1, which would mean the string is bad according to the manual, right? I've also tried screwing around with it, and

Code: Select all

$timeposted = date("Y-m-d g:i:s", $timeposted) ;
echos the old 1969-12-31 6:00:00 treatment. It's probably staring me in the face, but any help or advice would be helpful.

hope all is well. holy crap, sorry this is so long.
Last edited by imstupid on Thu Jan 19, 2006 3:12 pm, edited 1 time in total.
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

You don't need all this

Code: Select all

$ue_today = mktime(date("G"), date("i"), date("s"), date("m"), date("d"), date("Y"));
$timeposted = date ("Y-m-d G:i:s", $ue_today);
Just do this way

Code: Select all

$timeposted = date ("Y-m-d G:i:s", localtime());
But an easier way, since this being inserted into a datetime field of a database, would be to let the database handle it.
Set the field to NOW() (instead of $timeposted) and it should work just fine.

Sheila
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is your input field in the form actually named 'timeposted'? What it looks like is $_POST['timeposted'] is sending either null data or 0. That is why your date is showing as 1969-12-31.

Also, have you cleared your cache and echo'ed out the value of $_POST['timeposted']?
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post by imstupid »

Thanks for the help - it was actually my fault on an earlier page (I was using strtotime again, even though I used it on the previous page). Thanks anyway, appreciate the tips too.
Post Reply