mktime() abd TIMESTAMP

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
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

mktime() abd TIMESTAMP

Post by apek »

in my mysql...my data type for date i set to TIMESTAMP...
and then i use date() function to format it the way i like...
but i'm not sure about the mktime() function i'm using rite now...

this is my snippet of code:

Code: Select all

$mulaServis=$_POST["mulaServis"];
$tamatServis=$_POST["tamatServis"];
$start=mktime($mulaServis,$day, $month, $year);
$end=mktime($tamatServis,$day, $month, $year);
and then i use the $mula and the $tamat in my query to get data between the two date...i mean i want to fetch the data between the range $start and $end ..
this is my query:

Code: Select all

$viewall=query("Select * from preventiveMaint WHERE preventiveMaint.waktuTamat BETWEEN '$start' AND '$end'", $link, __LINE__, __FILE__);
my problem is i the query seems fail..
pls help me...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

It's a common pitfall when using MySQL timestamps. They aren't unix timestamps, they are just concatenated year,month,day,hours,minutes and seconds. Try the following:

Code: Select all

$mulaServis=$_POST["mulaServis"]; 
$tamatServis=$_POST["tamatServis"]; 
$start=date("YmdHis",strtotime($mulaServis));
$end=date("YmdHis",strtotime($tamatServis));
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

......

Post by apek »

thats working....
but..
:( ...
i have a date taken from a datepicker,the format of the date is like:
01/20/2004

my datatype for date in mysql is timestamp...
but why in the database the date will change to 00000000000000???

i heard mktime can port the date to timestamp format...or is it strtotime??

this is my try:

Code: Select all

$today=date("YmdHis",strtotime($_POST["tarikhMasukan"]));
and the $today wll be inserted to database...
if my code above is wrong,how to fix that?
i read about mktime and strtotime in php.net manual,but i dont understand because theres no sample there..only syntax....
pls help me...
pls help me...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

hmm, not sure about strtotime, but I do know mktime require arguments like this: mktime(hours,minutes,seconds,month,day,year). Each argument must be 2 characters long, except year which must be 4. Hope this helps.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

ok..but i test this
i want to convert date entered to time stamp format...

Code: Select all

$test=create_timestamp($_POST["tarikhMasukan"]);
then in custom function create_timestamp

Code: Select all

function create_timestamp()
{

  $timestamp = date("HismdY");

  return $timestamp;

}
and then the return result will be used in my INSERT INTO query:

Code: Select all

$sqlquery = query("INSERT INTO preventiveMaint(tarikhMasukan,noTag,idPekerja,waktuMula,masaMula,waktuTamat,masaTamat,hariBD,jamBD,minitBD,jenisOperasi,komponen,kuantiti,kos,maklumatKerja,jenis) VALUES('$test', '{$_POST["noTag"]}', '{$_POST["idPekerja"]}', '{$_POST["mulaServis"]}', '{$_POST["masaMula"]}', '{$_POST["tamatServis"]}', '{$_POST["masaTamat"]}', '{$_POST["hariBD"]}', '{$_POST["jamBD"]}', '{$_POST["minitBD"]}', '{$_POST["jenisOperasi"]}', '{$_POST["komponen"]}', '{$_POST["kuantiti"]}', '{$_POST["kos"]}', '{$_POST["kerja"]}','{$jenis}')");
but it is weird why in mysql database still showing
00000000000000???


?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

if you want to insert the current time as timestamp into a column, just use the NOW() function in mysql.

Thus "insert into table(atime) values (NOW())";

If you want to select it, use date_format
"select date_format(atime,'%Y-%m-%d %H:%i:%s') from table"
(More info about this function -> mysql.com/manual ;)

This way you avoid all problems that can arise ;)
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

but the problem is, i have alot more dates than today or NOW dates...
all of the above was just an example...
so how to do it? :cry:
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Post by mathewvp »

i have a date taken from a datepicker,the format of the date is like:
01/20/2004
You need to convert it into "Year month date format";

Code: Select all

$newdate=explode("/",$_POSTї"tarikhMasukan"]);    
$today=$newdateї2]."-".$newdateї0]."-".$newdateї1];

$start=date("YmdHis",strtotime($today));
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

its works!!!!

Post by apek »

its works!!!!

Code: Select all

$newdate=explode("/",$_POST["tarikhMasukan"]);   
$today=$newdate[2]."-".$newdate[0]."-".$newdate[1];
but what the [2],[0] and [1] mean?
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Post by mathewvp »

Your date from datepicker is in the format 01/20/2004 ie month/day/year.
explode splits it and stores month in $newdate[0],day in$newdate[1] and year in $newdate[2].You take that and make it into year-month-day format.ie 2004-01-20.
Post Reply