am using
$datetime = date_create()->format('Y-m-d H:i:s');
it is inserting into my database but - three hours.
for example my time is 09:30 pm it will insert 18:30.
Please help.Regards
Can you try this,
g 12-hour format of an hour without leading zeros (1 through 12)
G 24-hour format of an hour without leading zeros (0 through 23)
h 12-hour format of an hour with leading zeros (01 through 12)
H 24-hour format of an hour with leading zeros (00 through 23)
<?php echo date('Y-m-d H:i:s'');?> // 24 Hours format
Below are TimeZone converter,
PDT: America/Los_Angeles: <?php echo DatePNForServerTimeZone('D d M Y H:i:s A', 'America/Los_Angeles'); ?>
EST: America/New_York: <?php echo DatePNForServerTimeZone('D d M Y H:i:s A', 'America/New_York'); ?>
IST : Asia/Kolkata: <?php echo DatePNForServerTimeZone('D d M Y H:i:s A', 'Asia/Kolkata'); ?>
<?php
function DatePNForServerTimeZone($format='Y-m-d H:i:s', $Zone ='America/Los_Angeles'){
$utc = new DateTimeZone("UTC");
$new = new DateTimeZone($Zone);
$date = new DateTime(gmdate("m/d/Y H:i:s"), $utc);
$date->setTimezone($new);
return $date->format($format);
}
?>
Ref: http://php.net/manual/en/function.date.php
use date_default_timezone_set like following
date_default_timezone_set('Asia/Dhaka');
check the manual
Related
In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.
I currently have stored DateTime in the database as follows 20130208110000 so I need to break it into 2 parts which is date in this format 08/02/2013 and time in this format 11:00 AM, which function does this effectively?
This is the function I used to join both date and time together
$startMonth = $this->_getParam('starts_month_day_year');
$startTime = $this->_getParam('starts_time');
date("YmdHis",strtotime($startMonth.$startTime));
<?php
$originalDate = "20130208110000";
$newDate = date("d-m-Y g:i A", strtotime($originalDate));
echo $newDate; //08-02-2013 11:00 AM
?>
where,
g :12-hour format of an hour without leading zeros [0 through 12]
i :Minutes with leading zeros [00 through 59]
A:Uppercase Ante meridiem and Post meridiem [AM or PM]
What would be wrong with just using what you're already doing?
$myDate = "20130208110000";
date("d/m/Y", strtotime($myDate)); // Date, e.g. 08/02/2013
date("h:i A", strtotime($myDate)); // Time, e.g. 11:00 AM
Using PHP, I want to convert UNIX timestamps to date strings similar to this: 2008-07-17T09:24:17Z
How do I convert a timestamp such as 1333699439 to 2008-07-17T09:24:17Z?
Try gmdate like this:
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
use date function date ( string $format [, int $timestamp = time() ] )
Use date('c',time()) as format to convert to ISO 8601 date (added in PHP 5) - 2012-04-06T12:45:47+05:30
use date("Y-m-d\TH:i:s\Z",1333699439) to get 2012-04-06T13:33:59Z
Here are some of the formats date function supports
<?php
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>
Assuming you are using PHP5.3 then the modern way of handling dates is via the native DateTime class. To get the current time you can just call
$currentTime = new DateTime();
To create a DateTime object from a specific timestamp (i.e. not now)
$currentTime = DateTime::createFromFormat( 'U', $timestamp );
To get a formatted string you can then call
$formattedString = $currentTime->format( 'c' );
See the manual page here
It is very important to set a default timezone to get the correct result
<?php
// set default timezone
date_default_timezone_set('Europe/Berlin');
// timestamp
$timestamp = 1307595105;
// output
echo date('d M Y H:i:s Z',$timestamp);
echo date('c',$timestamp);
?>
Online conversion help: http://freeonlinetools24.com/timestamp
<?php
$timestamp=1486830234542;
echo date('Y-m-d H:i:s', $timestamp/1000);
?>
The DateTime class takes a string in the constructor. If you prefix the timestamp with a #-character you create a DateTime object with the timestamp. For formating use the 'c' format ... a predefined ISO 8601 compound format.
If could use the DateTime class like this ... set the right timezone or leave it out if you want a UTC time.
$dt = new DateTime('#1333699439');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('c');
https://www.php.net/manual/en/book.datetime.php
https://www.php.net/manual/en/datetime.format.php
more examples and timezones https://time.einfach.jetzt/?t=1333699439
$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);
This should work to.
You can do like as.....
$originalDate = "1585876500";
echo $newDate = date("Y-m-d h:i:sa", date($originalDate));
most people doesn't read comment, and there is problem with gmdate() in accepted answer, it return time in GMT, so if we use date_default_timezone_set(zone) it will not work, instead use date().
<?php
$ts = 1664706166;
$date = date('Y-m-d H:i:s', $ts);
$gmdate = gmdate('Y-m-d H:i:s', $ts);
echo "date() : $date\n";
echo "gmdate(): $gmdate\n\n";
date_default_timezone_set("Asia/Jakarta");
$date = date('Y-m-d H:i:s', $ts);
$gmdate = gmdate('Y-m-d H:i:s', $ts);
echo "date() : $date\n";
echo "gmdate(): $gmdate <- GMT\n";
/* result
date() : 2022-10-02 10:22:46
gmdate(): 2022-10-02 10:22:46
After set timezone
date() : 2022-10-02 17:22:46
gmdate(): 2022-10-02 10:22:46 <- GMT
*/
I found the information in this conversation so helpful that I just wanted to add how I figured it out by using the timestamp from my MySQL database and a little PHP
<?= date("Y-m-d\TH:i:s\+01:00",strtotime($column['loggedin'])) ?>
The output was: 2017-03-03T08:22:36+01:00
Thanks very much Stewe you answer was a eureka for me.
I am using $date = date("D M d, Y G:i");.
When I echo $date, it shows the correct date/time. Now I need this as an string.
I have tried string($date); but nothing happens here. And
$today = strtotime($date);
here I get weird numbers..
I need a string so I can put $today in a message.
What is the correct method for this?
The date() function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date variable, as a string -- no need for any additional operation.
If you like working with objects you can do this:
$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');
Your $date variable is a string, there's no need for any conversion.
You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date() function is string.
The strange numbers you see when you call strtotime() is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.
You're already getting a string. $date can be used like any string now.
strtotime() actually gives you the number of seconds in time like unix
$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;
With regards to:
$today = strtotime($date);
Those numbers are the current timestamp (the number of seconds since January 1st 1970).
You can use this as a second parameter in the date function to change the date to whatever you want.
$newDate = date("D M d, Y G:i", $timeStamp);
I am using following function and I want time in 24hr clock format but this gives me time in 12hrs:
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp = date("d/m/Y h:i:s", time());
print $timestamp ;
?>
What am I doing wrong?
From the docs for date(): The H format character gives the hour in 24h format. Also, you can use G if you do not want the leading 0 for hours before noon.
Examples (if current time was seven-something-AM)
date('H:i:s') -> "07:22:13"
date('G:i:s') -> "7:22:13"
For your specific case:
$timestamp = date("d/m/Y H:i:s", time());
According to the manual the difference is in the capitalization of hour portion:
"h" returns a 12-hour format of an hour with leading zeros, 01 through 12.
"H" returns a 24-hour format of an hour with leading zeros, 01 through 23.
According to the manual, G or H give you the time in 24-hour format. You should read the manual.
date('H', time());
The H format character gives the hour in 24h format. Also, you can use g if you do not want the leading 0 for hours before noon.
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));