473,561 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL DATETIME and PHP Dates

Hi,

Is there an easy way of formatting a MySQL DATETIME field in PHP. At the
moment I'm using the code below, as using the returned DATETIME value from
the database with the date() function doesn't seem to work. I know I could
use the MySQL function date_format to format it before its returned but I
use a simply query (e.g. SELECT * FROM TABLE WHERE colum = '$somevalue') and
a while loop to read all the variables in, so I don't think I can format it
before its returned.

$date1 = "2005-04-12 20:45:54";
$date_array = explode("-", $date1);
$year = $date_array[0];
$month = $date_array[1];
$day = $date_array[2];
$day = $day[0].$day[1];
$ts = mktime(0,0,0,$m onth, $day, $year);
$dateVal = date("j-M-y", $ts);
echo "<br>Date is: ".$dateVal;

Thanks,

T.
Jul 17 '05 #1
7 16195

Tony Clarke wrote:
Is there an easy way of formatting a MySQL DATETIME field in PHP. At the moment I'm using the code below, as using the returned DATETIME value from the database with the date() function doesn't seem to work. I know I could use the MySQL function date_format to format it before its returned but I use a simply query (e.g. SELECT * FROM TABLE WHERE colum = '$somevalue') and a while loop to read all the variables in, so I don't think I can format it before its returned.

$date1 = "2005-04-12 20:45:54";

Use the function strtotime() <http://www.php.net/strtotime>
echo '<br>Date is: ' . date('j-M-y',strtotime($d ate1));

Ken

Jul 17 '05 #2
NC
Tony Clarke wrote:

Is there an easy way of formatting a MySQL DATETIME field in PHP.


Yes. Check out the strtotime() function:

http://www.php.net/strtotime

Cheers,
NC

Jul 17 '05 #3
You could use the MySQL function UNIX_TIMESTAMP( column) to return a
PHP-compatible UNIX timestamp, which you could then pass as the second
parameter of the PHP date() function.

For example:

$row = mysql_fetch_ass oc(mysql_query( "SELECT UNIX_TIMESTAMP( datetime)
AS datetime_u FROM table LIMIT 1"));
$date = date('j-M-y', $row['datetime_u']);

Jul 17 '05 #4
Tony Clarke wrote on Wednesday 06 Apr 2005 23:37:
Is there an easy way of formatting a MySQL DATETIME field in PHP.


I've just solved this problem myself:

function reformatDate($d atetime) {
// put date in universal format, no seconds
list($year, $month, $day, $hour, $min, $sec) = split( '[: -]',
$datetime);
return "$year-$month-$day at $hour:$min";
}
$dateA=$row['date'];
$theDate=reform atDate($dateA);

Watch the line wrap in there!!

HTH,
- QS Computing.

--
QS Computing
http://www.qscomputing.plus.com
po********@qsco mputing.plus.co m
Jul 17 '05 #5
On 2005-04-07, QS Computing <po********@qsc omputing.plus.c om> wrote:
Tony Clarke wrote on Wednesday 06 Apr 2005 23:37:
Is there an easy way of formatting a MySQL DATETIME field in PHP.


I've just solved this problem myself:

function reformatDate($d atetime) {
// put date in universal format, no seconds
list($year, $month, $day, $hour, $min, $sec) = split( '[: -]',
$datetime);
return "$year-$month-$day at $hour:$min";
}
$dateA=$row['date'];
$theDate=reform atDate($dateA);


Or you could use the builtin functions for date/time handling:

$timestamp = "2005-04-06 15:43:34";
$time = strtotime($time stamp);
print date('Y-m-d \a\t H:i', $time)."\n";

The strtotime() function will parse the input and give you a unix
timestamp. The date() function is used to format unix timestamps in a
variety of ways.

--
Cheers
- Jacob Atzen
Jul 17 '05 #6
Jacob Atzen wrote on Thursday 07 Apr 2005 19:25:
Or you could use the builtin functions for date/time handling


Hadn't even thought of that. Presumably using builtins speeds it up - I'll
change my code now.

Thanks,
- QS Computing.

--
QS Computing
http://www.qscomputing.plus.com
po********@qsco mputing.plus.co m
Jul 17 '05 #7
On 2005-04-07, QS Computing <po********@qsc omputing.plus.c om> wrote:
Jacob Atzen wrote on Thursday 07 Apr 2005 19:25:
Or you could use the builtin functions for date/time handling


Hadn't even thought of that. Presumably using builtins speeds it up -
I'll change my code now.


Maybe so, maybe not. I doubt you'll be formatting that many dates that
it will actually make a difference speedwise. But it does make it easier
to parse and format dates in a variety of formats.

--
Cheers
- Jacob Atzen
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9455
by: Phil Powell | last post by:
I have a mySQL database table with a column field datatype of datetime that I set up in the original create table statement like this: create table nnet_usermetadata ( .... nnet_record_entered datetime default 'now()' ) However, upon execution, each time a record is entered into nnet_usermetadata the results are:
5
1513
by: Bob | last post by:
This little date formating problem is holding me up! Here is the sql query string. ..... WHERE (`work_orders`.`date_closed` > $ > $start_date) AND (`work_orders`.`date_closed` < $end_date) ORDER BY
2
1887
by: pholck | last post by:
A newbie question here: I'm linking to a MySQL backend table with a million+ records, where two fields are of type datetime. However Access considers those fields to be text. Can't seem to get Access to consider these date fields. Same thing happens when I import a (smaller) table into Access from MySQL - fields are converted to text. ...
2
2277
by: Kenneth P | last post by:
Hi, I'm trying to use MySql 4.1 db instead of MSSql2k and am having problems with the DateTime in the db. In mysqladmtools 1.0.19 I'm recommended to use 0000-00-00 00:00:00 as the default value, but you can't save these settings. If you read the manual they say the default value is thbe one above but that you can't use it, you have to...
0
1238
by: rlaemmler | last post by:
Hi, I just migrated my web app to .NET 2.0. Part of the app creates some business objects from a MySQL query which is returned by a web service. Some of those objects contain DateTime properties. Using .NET 1.1 soap serialized a DateTime always in the ISO8601 format. e.g. "2006-01-27T07:42:11.3014976-08:00". Now using .NET 2.0 suddenly the...
3
3423
by: laredotornado | last post by:
Hi, I have two columns, both MySQL 4 DATETIME types ... TABLE1.depart_day TABLE2.depart_day and both are indexed. The problem is, all of TABLE1's dates have a time of midnight (e.g. 2006-03-08 00:00:00" while TABLE2's columns have times (e.g. 2006-03-08 12:45:00). I want to match rows where TABLE1
6
38915
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a Unix-style timestamp and then formats it. $timestamp = "2005-04-06 15:43:34"; $time = strtotime($timestamp); print date('Y-m-d \a\t H:i',...
5
20825
by: rouven | last post by:
Hi, i am trying to convert that time format '05:26:40 Jun 19, 2007 PDT' into mysql compatible format like YYYY-MM-DD HH:MM:SS. the code i tried was: from datetime import datetime from time import strptime f1.write("strptime(%r); locale: %r" % (olddate, locale.getlocale())) newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z")...
2
2960
by: electronik | last post by:
I dont really have a problem, but I want to make my code faster and prettier. I have some tables with DATETIME in it. They have the std format %Y-%m-%d %H:%i:%s I need them to be in %d.%m.%Y %H:%i format. I tried to fix it with the server variable "datetime_format". After reading a lot in mysql doc I found out that: ...
0
7568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7614
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.