473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Convert Date Time into desired format

19
Dear Brothers,

I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As
MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used the MYSQL Tables DateTime Type as Text.

Time-formet 1. 04:02:27 16/01/2009
Time-formet 2. 16/01/2009 13:53:19
Time-formet 3. 00901E+13
Time-formet 4. Wed Jan 14 00:09:09 BDT 2009

I need to convert/change each date-time into j-M-y(14-Jan-09) H:i:s(00:00:00) format.

For converting Time-formet 1. I have used the following PHP code.

Expand|Select|Wrap|Line Numbers
  1.   $vocc_dt="04:02:27 16/01/2009";  
  2.   $vrec_dt="08:02:27 16/01/2009";
  3.   $occ_dt=strtotime($vocc_dt);
  4.   $rec_dt=strtotime($vrec_dt);
  5.   $for_vocc_d = date('j-M-y',$occ_dt);
  6.   $for_vocc_t = date('H:i:s',$occ_dt);
  7.   $for_vrec_d = date('j-M-y',$rec_dt);
  8.   $for_vrec_t = date('H:i:s',$rec_dt);
  9.  
  10.    echo "Occurance Date Time: $for_vocc_d $for_vocc_t<br>";
  11.    echo "Recovery Date Time : $for_vocc_d $for_vocc_t<br>";
  12.    //This script gives invalid date time like 1-Jan-70 00:00:00 but if i use like below..
  13.    $vocc_dt="04:02:27 1/16/2009";  
  14.    $vrec_dt="08:02:27 1/16/2009";              
  15.     //it gives accurate output....
  16.  
  17.    //for time difference I have used the following php code:
  18.     function date_diff($d1, $d2){
  19.     $d1 = (is_string($d1) ? strtotime($d1) : $d1);
  20.     $d2 = (is_string($d2) ? strtotime($d2) : $d2);
  21.     $diff_secs = abs($d1 - $d2);
  22.     $base_year = min(date("Y", $d1), date("Y", $d2));
  23.     $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
  24.     return $diff_secs;    
  25.     }
  26.  
  27.   $vduration = date_diff("$vocc_dt", "$vrec_dt");
  28.   $unith =3600;      
  29.   $unitm =60;         
  30.   $hh = intval($vduration / $unith);    
  31.   $ss_remaining = ($vduration - ($hh * 3600));    
  32.   $mm = intval($ss_remaining / $unitm);   
  33.   $ss = ($ss_remaining - ($mm * 60));
  34.   //It also gives the accurate duration. 
  35.  
I have spent much time to change these time formats into my desired format (14-Jan-09 00:00:00).Please give me an efficient
solution for these (4 types) date time types.
I am waiting for your valuable help.

Kind Regards,
A H MUrad
Jan 17 '09 #1
4 32927
Atli
5,058 Expert 4TB
Hi.

All you need to do, really, is to convert each date into a unix timestamp.
The strtotime function can do that for you, provided that you can provide a date in a common format.
Then you can use the date function to get them into whatever format you need.

The first two formats you had are almost correct. Just replace the forward-slashes (/) with dashes (-) and run them through strtotime().

The third and the fourth are already in valid format. Just remember that the third is a float, so it should not be used as a string.
Jan 17 '09 #2
ahmurad
19
Dear Brother Atli,

Thank you very much. Really your reply was helpful for me. Here I had to do an additional task besides your advice to convert the DB time string in US time format (d-m-y to m-d-y) , as strtotime() expects m-d-y format.

Thanks the group members also.

Regards
Murad
Jan 21 '09 #3
Atli
5,058 Expert 4TB
Glad I could help.

I did create a little example code tho, showing the most efficient way (I can think of) to do this. An alternative method, if you will:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Content-Type: text/plain");
  3.  
  4. // Target format
  5. $format = "j-M-y H:i:s";
  6.  
  7. // Unformatted dates
  8. $f1_raw = "04:02:27 16/01/2009";
  9. $f2_raw = "16/01/2009 13:53:19";
  10. $f3_raw = "00901E+13";
  11. $f4_raw = "Wed Jan 14 00:09:09 BDT 2009";
  12.  
  13. // Convert the first and second
  14. $f1_formatted = date($format, strtotime(str_replace("/", "-", $f1_raw)));
  15. $f2_formatted = date($format, strtotime(str_replace("/", "-", $f2_raw)));
  16.  
  17. // Convert third
  18. $f3_formatted = date($format, strtotime((float)$f3_raw));
  19.  
  20. // Convert fourth
  21. $f4_formatted = date($format, strtotime($f4_raw));
  22.  
  23. // Print the results
  24. echo "Format 1: $f1_formatted".PHP_EOL;
  25. echo "Format 2: $f2_formatted".PHP_EOL;
  26. echo "Format 3: $f3_formatted".PHP_EOL;
  27. echo "Format 4: $f4_formatted".PHP_EOL;
  28. ?>
Which prints:
Expand|Select|Wrap|Line Numbers
  1. Format 1: 16-Jan-09 04:02:27
  2. Format 2: 16-Jan-09 13:53:19
  3. Format 3: 21-Jan-09 04:01:00
  4. Format 4: 14-Jan-09 10:09:09
Jan 21 '09 #4
ahmurad
19
Yes !! your code is most efficient. I have done this task another 2 ways. But thats the most efficient, easiest solution !! I accepted.

Thnx brother !!
Jan 22 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marauderz | last post by:
Hello guys, got a little question regarding the regional language settings . Anyway I got a Windows 2003 Server machine that was installed with the date format "dd/MM/yyyy" and location still...
1
by: Ryan Ramsey | last post by:
I am trying to convert a value returned from the date() function in php 5.0 to a format .NET can use. DateTime dt_now = DateTime.Now; DateTime dt_last = new DateTime(Convert.ToInt32(dkpLast));...
5
nirmalsingh
by: nirmalsingh | last post by:
i am getting date format in string as 30-11-2006 05:59:44 PM . i want to convert in the format of yyyy-mm-dd hh:mm:ss to sore it in mysql database. Help me with sample coding plz..
4
by: Brad | last post by:
Is there a way to determine the OS's date/time culture format (e.g. mm/dd/yyy vs. dd/mm/yyy) setting at run time? TIA Brad
1
by: nitinkeser | last post by:
how i convert date into specific format in asp? like in date format 'Jun 17, 2007' ?
4
by: RP | last post by:
I am using SQL Server 2005 as backend. I have a Text Box that accepts Date in the format dd-MM-yyyy. But when I attempt to insert a record, an error is displayed: Cannot convert char to DateTime...
4
omerbutt
by: omerbutt | last post by:
hi there i am inserting a desired formated date ie m-d-y --------2-23-2008 into mysql database in which i have the field named Bill_date which has the format Date here is my query ...
1
by: billa856 | last post by:
Hi, I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date. I am using "" to insert Null in that column(ShipDate)...
3
by: ssmeshack | last post by:
Hi all, Im have a problem here. Im using VWD 2008 (c#) and Mysql 2005. I dont know how to convert datetime data that i retrieve from mysql database to only date or time. Can anyone help me? Here...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.