Using PHP to insert/output dates and time in mysql? - php

I don't know why it is so confusing, maybe it s because there are so many ways to output/input date and time with mysql and php. All I want is for users to put a date in this format
MM/DD/YYYY
and for it to output in this format
Month Name - Date
and for time, I want users to pick the time from a select field, starting with 1:00 P.M. and incrementing down 30 minutes, so it will be 1:30 P.M., 2:00 P.M... and I want to somehow insert this into MySQL and output it as 1:30 P.M. Not sure how to do that either.

The best way to store a date format in a database and be able to display it the way you want is to use the time() and date() function.
when storing the date in your database you should use time(), it will generate a string like this -> 1300695900. this little string contains the date and time
then you can use date to display it in any way you want:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
and so on...
Edit:
to answer your last question, you just get the different values, stick it together (concatenation) and then use strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30

Related

Convert Date and Time to other Date and Time with PHP

I am getting date in format 2019-11-26T16:30:00+01:00 from an API and want to convert it to a format like Di. 26. Nov. 2019 / 16:30.
I am usinig following code, but always get 1 hour difference: Di. 26. Nov. 2019 / 15:30 Uhr.
If I use 2019-11-26T16:30:00+02:00 - I get Di. 26. Nov. 2019 / 14:30 Uhr (2 hours earlier).
Here is my php code:
$timstp = strtotime('2019-11-26T16:30:00+1:00');
echo date('d.m.Y H:i:s', $timstp);
How can I get correct date?
Use the DateTime class. The format method returns the result in English.
$input = '2019-11-26T16:30:00+1:00';
$date = date_create($input)->format('D. d. M. Y /H:i \U\h\r');
echo $date; //Tue. 26. Nov. 2019 /16:30 Uhr
For an output in other languages ​​like German I recommend the DateTime extension class dt.
$input = '2019-11-26T16:30:00+1:00';
$date = dt::create($input)->formatL('D d. M Y / H:i \U\h\r','de_DE');
echo $date; //Di. 26. Nov. 2019 / 16:30 Uhr
Update:
Does the API get entries from different time zones? If so, is the question what is needed?
The local time of the time zone or a unique time base for comparability?
The examples above show the local time of the time zone.
To create a uniquet basis, the DateTime object can be converted to a different time zone how UTC.
$input = '2019-11-26T15:30:00+5:00';
$date = date_create($input)
->setTimeZone(new DateTimeZone('UTC'))
->format('D. d. M. Y / H:i:s')
;
echo $date; //Tue. 26. Nov. 2019 / 10:30:00
try this
$datetime = new DateTime('2019-11-26T16:30:00+1:00');
echo $datetime->format('d.m.Y H:i:s');
find more details about DateTime here

DateTime formate in php

I have date stored in database in this format frankly i dont know the timezone of this format
2016-05-26T11:35:00.000Z
but i want to display date in this format
May 27, 2016 12:00 am
Here is code
$date = substr($query_result->post_date, 0,10);
$date = date_create($date);
date_format($date,"F j, Y g:i a");
It shows date correctly but it always shows 12:00 am with date.
Like if date is
2016-05-26T11:35:00.000Z
then it should display
May 26, 2016 11:35 am/pm
Thanks in advance
While shrinking your datetime string, you lose time and timezone information. Time 00:00 and system timezone is then applied to your datetime, and you get wrong result because of that.
$date = date_create($query_result->post_date);
date_format($date,"F j, Y g:i a");

PHP - Want to convert MySQL Timestamp time to UTC +6 time

I have an old MySQL database. Here is a time column.
I see here is some time values Like:
2013-06-03 21:33:15
So, I want to convert this time to my local time UTC +6 in my PHP Script.
How can it possible?
I can make a mysql query to get the time from Database to my my PHP variable $TimeFromMySQL
Just now I want to show like:
11:32:44 PM 05 July 2014
Thank You
See VMai's comment above if you want to do this in MySQL. For PHP:
$inDate = '2013-06-03 21:33:15';
$inDate_tz = 'America/Chicago';
$original_date = new DateTime($inDate, new DateTimeZone($inDate_tz) );
$original_date->setTimeZone(new DateTimeZone('Asia/Dhaka'));
$new_date = $original_date->format('H:i:s d F Y');
echo $new_date;
//outputs 08:33:15 04 June 2013
My answer here might be too late, still it might be helpful for some one who run to the same situation like me before I work around this solution.
To convert datetime to UTC that is, getting correct location Time and Date.
I came up with this:
// Get time Zone.
$whereTimeNow = date_default_timezone_get();
// Set the time zone.
date_default_timezone_set($whereTimeNow);
$dateTime = date('d-m-Y H:i');
$dateTimeConvert = new DateTime($dateTime, new DateTimeZone($whereTimeNow) );
$dateTimeConvert->setTimeZone(new DateTimeZone('UTC'));
$dateTimeNow = $dateTimeConvert->format('Y-m-d H:i:s');
echo $dateTimeNow;

how to php convert this time format?

i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php

How to convert time like 2 pm or 3 am to HH:MM:SS format in php?

How can I convert times like 2 pm or 3 am to HH:MM:SS format in php?
Like this:
$date = '2pm';
echo date('H:i:s', strtotime($date));
Result:
14:00:00
And:
$date = '2am';
echo date('H:i:s', strtotime($date));
Result:
02:00:00
More Info:
date
strtotime
Update:
To convert it back:
$date = '14:00:00';
echo date('HA', strtotime($date));
Result:
14PM
(this should be a comment, but it's too long)
The reason Sarfraz's solution is incomplete is because it doesn't account for DST transaitions. During DST transitions, some hours may not exist.
Consider the timezone is Europe/Lisbon and we're in March 28th 2010, when DST kicked in.
When we hit 1am, we change from UTC+0 to UTC+1, i.e., we skip 1 hour. Example:
date_default_timezone_set("Europe/Lisbon");
$date = '2010-03-28 1am';
$date2 = '2010-03-28 1:30am';
echo date('H:i:s', strtotime($date)),"\n";
echo date('H:i:s', strtotime($date2)),"\n";
gives
02:00:00
02:30:00
Therefore, Sarfraz solution will fail unless when you say you want to convert 1am to 01:00, these times always refer to the current day in the server's timezone.

Categories