I want to add content to a MySQL table with current date and time.
When I insert the content to database it shows the correct date but the wrong time.
$date1 = date("Y-m-d H:i:s");
date_default_timezone_set('Asia/Kolkata');
$date1 = date("Y-m-d H:i:s");
Asia/calcutta has changed to Asia/Kolkata
[a] strftime(): Format a local time/date according to locale settings
[b] date : Format a local time/date
Examples
Consider following simple example:
<?php
print strftime('%c');
?>
Output:
Mon Apr 23 01:22:58 2007
You need to pass format such as %c to strftime() to print date and time representation for the current system. You can use following format characters:
%m - month as a decimal number (range 01 to 12)
%d - day of the month as a decimal number (range 01 to 31)
%Y - year as a decimal number including the century
You can see the complete format conversion specifiers online here
You can also use date() as follows:
<?php
print date('r');
print "\\n";
print date('D, d M Y H:i:s T');
print "\\n";
?>
Output:
Mon, 23 Apr 2007 01:29:56 +0530
Mon, 23 Apr 2007 01:35:14 IST
Related
I need to convert the date from 2020-11-01 to string 01 Nov 2020
So far I could achieved like this
$test = DateTime::createFromFormat('Y-m-d', '2020-11-01')->format('d m Y');
But the result is show 01 11 2020
How to make the month become Nov?
date('d M Y',strtotime('2020-11-01'));
you can use strtotime to get the time stamp and the use that time stamp to change format with date funtion
$timeStamp=strtotime("2020-11-01");
$res=date("d M Y",$timeStamp);
I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020
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
I try to convert time to my local time (Asia/Jakarta)
this is my Current Milliseconds : 1507539987576
i want convert to like this format : Mon Oct 09 16:06:27 WIB 2017
this is my code using php
date_default_timezone_set("Asia/Jakarta");
$now = new DateTime();
$nowadays = date('Y-m-d');
$time = round(microtime(true) * 1000);
$seconds = $time / 1000;
$currdate = date("Y-m-d H:i:s", $seconds);
echo $currdate;
and the result like this : 2017-10-09 16:06:27
can someone tell me how to convert the time like this format : Mon Oct 09 16:06:27 WIB 2017
The PHP documentation outlines all of the format characters. The string that you require to parse the date in the format you've indicated (i.e. Mon Oct 09 16:06:27 WIB 2017) is as follows:
date( 'D M d H:i:s T Y' );
We can break this down as follows:
D: A textual representation of a day, three letters
M: A short textual representation of a month, three letters
d: Day of the month, 2 digits with leading zeros
H: 24-hour format of an hour with leading zeros
i: Minutes with leading zeros
s: Seconds, with leading zeros
T: Timezone abbreviation
Y: A full numeric representation of a year, 4 digits
I also do not understand why you're working with microtime and then calculating the seconds. By default, date() will always use the current time, or you can simply use time() instead of your calculations.
I need to get data from RSS feed and then save it to MySQL. The problem is that in RSS feed datetime format is like this: Sun, 09 Nov 2014 12:00:38 +0200 How I could convert it to format so I could save it to database? and how to convert it back later when I want to display it again with the same format?
Try this
$DateTime= date("Y-m-d H:i:s", strtotime("Sun, 09 Nov 2014 12:00:38 +0200"));
echo $DateTime;
To retrieve back from db, in your select query use
DATE_FORMAT(date_column, '%a %d %b %Y %T')
if you are using PHP along with MySQL, strtotime() is nice php function :)
http://php.net/manual/en/function.strtotime.php
date_default_timezone_set('UTC');
$date_string = 'Sun, 09 Nov 2014 12:00:38 +0200';
echo 'original string: '.$date_string.'<br/>';
$unix_time_stamp = strtotime($date_string );
echo 'timestamp: '.$unix_time_stamp.'<br/>';
$old_format = date("D, j M Y H:i:s O", $unix_time_stamp );
echo 'back to originalt: '.$old_format;
Example on http://viper-7.com/KI5LfG
You can get any date format you desire with php date()
http://php.net/manual/en/function.date.php