473,785 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert Date Time into desired format

19 New Member
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).Pleas e 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 32957
Atli
5,058 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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
5919
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 locked in US. As we migrated certain apps over to the new machine. we found out something... someone coded a Now.ToString in a database query... and the SQL Box is running MM/dd/yyyy! Chaos follows!!! I tried setting the Administrator account's...
1
2950
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)); TimeSpan dt_diff = dt_now.Subtract(dt_last); the value returned to dt_last is 1153455444 which is rougly July 20th ~ 10pm When I compare this to dt_diff I get a delta value (via dt_diff.Days) of
5
19188
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
2827
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
12971
by: nitinkeser | last post by:
how i convert date into specific format in asp? like in date format 'Jun 17, 2007' ?
4
2496
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 .... Actually, when the text box has entry like 22-10-2005, it considers 22 as month whereas it is DD. How to convert the Text Box value to an acceptable format. I tried: DateTime.parse(txtDOB.Text,"dd-MM-yyyy")
4
4553
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 $sqlAS="insert into sale_rec(Bill_no,Bill_date,Cust_name,Mop,Con_per,Con_no,Code_no,Prt_no,Stk_avl,Stk_req,U_prc,T_prc,Prd) ...
1
3763
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) but it shows warning that customer can't append all the records in the append query. Customer set 1 field(s) to Null due to a type conersion failure,and it didn't add 0 record(s) to the table due to key violation, 0 record(s) due to lock...
3
2467
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 is something that i tried to do. Date1.Text = Convert.ToString(reader, (MMMM d, yyyy)); //"MMMM d, yyyy", Time1.Text = Convert.ToString(reader, (HH:mm ss tt)); //"HH:mm ss tt", but it didnt work even when i try to put format in of reader.
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7502
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.