Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MSSQL date time format

Posted on 2006-10-25
18
Medium Priority
?
23,163 Views
Last Modified: 2011-08-18
ive got a MSSQL date / time format like:

Oct 24 2006 12:00AM

im trying to change it so it displays like:

24/Oct/2006 using the mktime() but im getting:

Warning: mktime() expects parameter 4 to be long, string given in c:\Inetpub\wwwroot\prelease\index.php on line 549
01/Jan/1970

My code:

echo date('d/M/Y',mktime(0,0,0,$datein));
0
Comment
Question by:ellandrd
18 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 680 total points
ID: 17801789
>ive got a MSSQL date / time format like:
>Oct 24 2006 12:00AM
do you actually have a datetime field or a varchar field?

on a datetime field, use the convert in the query itself:

select convert(varchar(40), yourfield, 106) from yourtable
resp:
select replace(convert(varchar(40), yourfield, 106) , ' ' , '/') from yourtable


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 680 total points
ID: 17801792
You could try working with strtotime, but I doubt it will accept that date format. strptime should when your locale is set on english

Try:

$array = strptime($date, '%b %e %Y %H:%M%r'); //(when using Oct 1 2006)
or
$array = strptime($date, '%b %d %Y %H:%M%r'); //(when using Oct 01 2006)

$stamp = mktime($array['tm_hour'], $array['tm_min'], $array['tm_sec'], $array['tm_mon'], $array['tm_mday'], $array['tm_year']);

echo date('d/M/Y/, $stamp);

-r-
0
 
LVL 18

Assisted Solution

by:Mark Gilbert
Mark Gilbert earned 640 total points
ID: 17801868
I would recommend against changing your timestamp field.  Keep it in your database and update the time by using update tablename set mydatefield = NOW() where myotherfield = y;

When formatting the string, the database has very powerful formatting capabilities.  The sql I would use for the above would look like:

select date_format(mydatefield, '%b %d %Y %h:%i%p') as formatteddate from tablename where myotherfield = 'foo'; and that would give you an output of Oct 24 2006 12:00AM.  There are many other formatting parameters that you can give the date_format function.

I find it is best to use the database whereever possible to do date and time formats, along with date and time calculations.  This then frees up php to actually process the other data.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Author Comment

by:ellandrd
ID: 17801876
>>do you actually have a datetime field or a varchar field?

datetime

>>select convert(varchar(40), yourfield, 106) from yourtable...

will convert(varchar(40), ...) will in a query string for PHP?

>>You could try working with strtotime

I will take a look at this...
0
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 17801881
%b = Abbreviated month name (Jan..Dec)
%d = Day of the month, numeric (00..31)
%Y = Year, numeric, 4 digits
%h = Hour (01..12)
%i = Minutes, numeric (00..59)
%p = AM or PM
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17801955
>>I would recommend against changing your timestamp field.  Keep it in your database and update the time by using update tablename set mydatefield = NOW() where myotherfield = y;

do you want my Senior Programmer to kill me.  Our database is used by a large number of people and other web applications so changing it will cause more problems that good so your answer is no.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17801970
Roonaan,

Im getting:

Call to undefined function strptime()

Im running PHP 5.1.4
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17802031
AngelIII

when i try using convert() i get this:

Warning: mssql_result() [function.mssql-result]: DateIn field not found in result in c:\Inetpub\wwwroot\prelease\index.php on line 543

My query:

$query = "SELECT CONVERT(varchar(40),DateIn,106) FROM tblBillables, tblItem WHERE tblBillables.BillableID = tblItem.BillableID AND tblBillables.NameID = ".$_SESSION['user_info']['persid']." AND tbl.Item.BillableID = ".$_GET['bid']." ORDER BY tblItem.Day, tblItem.Month";
            
0
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 17802170
Ellandrd, by running the convert you are changing your datetime field to a varchar with a length of 40.  If your Senior Programmer is going to kill you, then I suggest you start running immediately because he's after you...Lol.

By running the date_format() code as provided, you will find that your formatting of the information will come out nicely.  I must clearly state though that by running the date_format function, you will not change any data in the database, you are merely changing the way it's outputted at runtime.  For example:

select date_format(mydatefield, '%b %d %Y %h:%i%p') as formatteddate from tablename where myotherfield = 'foo';

and select date_format(mydatefield, '%M %d %Y %H:%i:%s') as formatteddate from tablename where myotherfield = 'foo'

will return two seperate formats of the date, but the data in the data will remain unchanged.

I would never provide code that would change the integrity of your data...just the way it looks when you output it.

Hope this helps.
0
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 17802173
but the data in the data should read but the data in the database.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17802179
ok i will give this a try...

0
 
LVL 16

Author Comment

by:ellandrd
ID: 17802240
Warning: mssql_query() [function.mssql-query]: message: 'date_format' is not a recognized function name. (severity 15) in c:\Inetpub\wwwroot\prelease\index.php on line 542
0
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 17802332
umm, sorry it should be uppercase.  select DATE_FORMAT(`mydatefield`, '%b %d %Y %h:%i%p') as formatteddate.

If that doesn't work, what version of mysql are you running?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17802390
its not MYSQL its MSSQL
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17802564
Ok ive managed to kinda workaround it using explode().  its probably not the best or most efficient way but none of the solution/ functions provided seem to work.

my "workaround" is as follows:

$parts = explode(' ',$datein);
echo $parts[1].'/'.$parts[0].'/'.$parts[2];

Thanks to everyone who took some time to help me...

ellandrd

p.s the accepted comment was not given to any particular expert for any reason, i just selected the first reply as i have to select an accepted comment as we all know... points split.
0
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 17802727
ellandrd, sorry I wasn't able to provide specific information for mssql...hadn't correctly read the question...but thanks for the split...hopefully my example can help you in the future when you use mysql.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 17802801
oh yes, im sure it will.  the functions and advice i got today will be referred back to when i have more issues.

i alway take on board any help i get from EE experts so next time i dont have to ask a question - instead start answering more of them for others! its a great learning curve  - hey? ;-)

ellandrd
0
 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 17803282
Definately a good learning curve...I don't know what I would have done without EE to begin with.  Good luck with your project, and learn hard :)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question