Solved

Echo time from date/time field.

Posted on 2006-11-29
8
240 Views
Last Modified: 2013-12-12
Hi.

I have a date/time field in mysql database and i want to echo just the time and then just the month...etc

example :
field "chronos" data : 2006-11-29 16:00:00

echo time(chronos) = 16:00
echo year(chronos) = 2006
echo month(chronos) = 11
echo date(chronos) = 29

Thanks in advance.
0
Comment
Question by:CodedK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 18042491
if you are doing this in mysql you could just do these as part of the query.

ie

select time(chronos) as tc, year(chronos) as yc, month(chronos) as mc, dayofmonth(chronos) as dc from tableName

0
 
LVL 35

Expert Comment

by:Raynard7
ID: 18042497
or you could format the date in the form

string date ( string format [, int timestamp] )

using the arguements located at http://au2.php.net/manual/en/function.date.php
0
 
LVL 16

Author Comment

by:CodedK
ID: 18042542
I want to do it with php and i dont know how date function can help.
I mean i want to extract for example only the hour from a given string and after only the year...


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:alex_code
alex_code earned 55 total points
ID: 18042550
Hi Codedk

first of all you have set a field with datetime property as i understand so the result is 0000-00-00 00:00:00 and you want to take several parts like only time or only date e.t.c.

one way to do this is with sql commands for example:

SELECT DATE_FORMAT('2006-12-31 04:23:00 or you can put the dynamic data from the field datetime', '%W %M %Y');
       this is gone give you 2006-12-31  

SELECT DATE_FORMAT('2006-12-31 04:23:00', '%H:%i:%s');
      this is gone give you '04:23:00'

SELECT DATE('2006-12-31 04:02:03');
this is gone give you 2006-12-31  

hope this to help you!

Regards

Alex_Code
0
 
LVL 9

Expert Comment

by:alex_code
ID: 18042601
Oh, Codedk sorry...

you said that you want it in php...

So try this:

echo date("M", mktime()-$chronos);
echo date("Y", mktime()-$chronos);
echo .... e.t.c.

i$chronos  is your variable as you said to your post...
0
 
LVL 8

Expert Comment

by:John Kawakami
ID: 18044711
mktime()-$chronos?

I think you mean strtotime($chronos).

It's probably more efficient to split it up in SQL.  strtotime() is a "smart" algorithm that converts English strings into timestamps.  As you can imagine, it's complex.  With SQL, the server knows the format, so parsing the parts is simpler.
0
 
LVL 14

Expert Comment

by:ygoutham
ID: 18045100
you already have it in date/time format in mysql. all you need is to select it with date_format(chronos, '%r %H')

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

see the page and do a search for Date_Format and you see a table to select whatever you want from the field.  use a combo and get your results
0
 
LVL 14

Accepted Solution

by:
ygoutham earned 70 total points
ID: 18045276
you can do

select date_format(chronos, '%r,H=%H,h=%h,%Y,%y,%m,%M') from table_name;

this gives a "," separated value for you to work with in php which you can use to explode and get as individual values
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

688 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