• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 23226
  • Last Modified:

MSSQL date time format

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
ellandrd
Asked:
ellandrd
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
 
RoonaanCommented:
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
 
Mark GilbertSenior Performance EngineerCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ellandrdAuthor Commented:
>>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
 
Mark GilbertSenior Performance EngineerCommented:
%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
 
ellandrdAuthor Commented:
>>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
 
ellandrdAuthor Commented:
Roonaan,

Im getting:

Call to undefined function strptime()

Im running PHP 5.1.4
0
 
ellandrdAuthor Commented:
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
 
Mark GilbertSenior Performance EngineerCommented:
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
 
Mark GilbertSenior Performance EngineerCommented:
but the data in the data should read but the data in the database.
0
 
ellandrdAuthor Commented:
ok i will give this a try...

0
 
ellandrdAuthor Commented:
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
 
Mark GilbertSenior Performance EngineerCommented:
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
 
ellandrdAuthor Commented:
its not MYSQL its MSSQL
0
 
ellandrdAuthor Commented:
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
 
Mark GilbertSenior Performance EngineerCommented:
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
 
ellandrdAuthor Commented:
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
 
Mark GilbertSenior Performance EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now