PHP DateTime conversion - php

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

Related

how do you convert the "exp" jwt to proper date format(YYYY-MM-DD T HH:MM+02 00) [duplicate]

I have a timestamp stored in a session (1299446702).
How can I convert that to a readable date/time in PHP? I have tried srttotime, etc. to no avail.
Use PHP's date() function.
Example:
echo date('m/d/Y', 1299446702);
strtotime makes a date string into a timestamp. You want to do the opposite, which is date. The typical mysql date format is date('Y-m-d H:i:s'); Check the manual page for what other letters represent.
If you have a timestamp that you want to use (apparently you do), it is the second argument of date().
I just added H:i:s to Rocket's answer to get the time along with the date.
echo date('m/d/Y H:i:s', 1299446702);
Output: 03/06/2011 16:25:02
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new \DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
result: 2016-06-07 14:29:00
Other time zones:
Africa
America
Antarctica
Arctic
Asia
Atlantic
Australia
Europe
Indian
Pacific
Others
If you are using PHP date(), you can use this code to get the date, time, second, etc.
$time = time(); // you have 1299446702 in time
$year = $time/31556926 % 12; // to get year
$week = $time / 604800 % 52; // to get weeks
$hour = $time / 3600 % 24; // to get hours
$minute = $time / 60 % 60; // to get minutes
$second = $time % 60; // to get seconds
If anyone wants timestamp conversion directly to a DateTime object, there's a simple one-liner:
$timestamp = 1299446702;
$date = DateTime::createFromFormat('U', $timestamp);
Following #sromero comment, timezone parameter (the 3rd param in DateTime::createFromFormat()) is ignored when unix timestamp is passed, so the below code is unnecessary.
$date = DateTime::createFromFormat('U', $timestamp, new DateTimeZone('UTC'); // not needed, 3rd parameter is ignored
You may check PHP's manual for DateTime::createFromFormat for more info and options.
Try this one:
echo date('m/d/Y H:i:s', 1541843467);
$epoch = 1483228800;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00
In the examples above "r" and "Y-m-d H:i:s" are PHP date formats, other examples:
Format Output
r ----- Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date)
c ----- 2017-03-15T12:00:00+01:00 (ISO 8601 date)
M/d/Y ----- Mar/15/2017
d-m-Y ----- 15-03-2017
Y-m-d H:i:s ----- 2017-03-15 12:00:00
Try it.
<?php
$timestamp=1333342365;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
You can try this:
$mytimestamp = 1465298940;
echo gmdate("m-d-Y", $mytimestamp);
Output :
06-07-2016
Unless you need a custom date and time format, it's easier, less error-prone, and more readable to use one of the built-in date time format constants:
echo date(DATE_RFC822, 1368496604);
echo date("l M j, Y",$res1['timep']);
This is really good for converting a unix timestamp to a readable date along with day. Example:
Thursday Jul 7, 2016
echo 'Le '.date('d/m/Y', 1234567890).' à '.date('H:i:s', 1234567890);
I have used this:
<?php echo date('d/m/Y H:i a', $row['start_time']); ?>

How to use date() and strtotime() to get a date from a string?

I am taking input following Datetime : 12.01.2017 2:25 pm
But while I passed above datetime on php function strtotime then it's doesn't returning exact datetime as 24 hours format.
Here is my scripts:
$sdate="12.01.2017 2:25 pm";
date("d.m.Y H:m", strtotime($sdate))
It's returning always 12-jan-2017 14:01:00
But accurate output should be : 12-jan-2017 14:25:00
Please let me know how can get accurate datetime.
Thanks
hey there is problem on your dateformat string :
it should be H:i instead of H:m on your scripts.
Please correct date and format it accordingly your expectation:
date("d-M-Y H:i", strtotime($sdate));
<?php
$sdate="12.01.2017 2:25 pm";
echo date("d-M-Y H:i", strtotime($sdate));
?>
Output
You should use i instead of m in the date:
like this
echo $sdate="12.01.2017 2:25 pm";
echo date("d.m.Y H:i", strtotime($sdate));
There are already answers that use strtotime(). I would like to show you the DateTime class. I find it very usefull and better looking in general. Not that it matters to most people but still.
// Date string
$dateString = '12.01.2017 11:03 am';
// Create date object from string
$oDate = DateTime::createFromFormat('d.m.Y h:i a', $dateString);
// Output the date
echo $oDate->format('d.m.Y h:i a');
// Or some other format
echo $oDate->format('Y-m-d H:i:s');
Note that date() and DateTime accept the same date parameters. Your string suggests that you will need the following parameters.
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
h - 12-hour format of an hour with leading zeros
i - Minutes with leading zeros
a - Lowercase Ante meridiem and Post meridiem
More information
For more information about date, DateTime, strtotime() and all parameters you can use:
date() - Manual
DateTime - Manual
strtotime() - Manual
date() and DateTime manuals list all possible parameters

php format date() to 24 hour format when inserting

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

Convert timestamp to readable date/time PHP

I have a timestamp stored in a session (1299446702).
How can I convert that to a readable date/time in PHP? I have tried srttotime, etc. to no avail.
Use PHP's date() function.
Example:
echo date('m/d/Y', 1299446702);
strtotime makes a date string into a timestamp. You want to do the opposite, which is date. The typical mysql date format is date('Y-m-d H:i:s'); Check the manual page for what other letters represent.
If you have a timestamp that you want to use (apparently you do), it is the second argument of date().
I just added H:i:s to Rocket's answer to get the time along with the date.
echo date('m/d/Y H:i:s', 1299446702);
Output: 03/06/2011 16:25:02
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new \DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
result: 2016-06-07 14:29:00
Other time zones:
Africa
America
Antarctica
Arctic
Asia
Atlantic
Australia
Europe
Indian
Pacific
Others
If you are using PHP date(), you can use this code to get the date, time, second, etc.
$time = time(); // you have 1299446702 in time
$year = $time/31556926 % 12; // to get year
$week = $time / 604800 % 52; // to get weeks
$hour = $time / 3600 % 24; // to get hours
$minute = $time / 60 % 60; // to get minutes
$second = $time % 60; // to get seconds
If anyone wants timestamp conversion directly to a DateTime object, there's a simple one-liner:
$timestamp = 1299446702;
$date = DateTime::createFromFormat('U', $timestamp);
Following #sromero comment, timezone parameter (the 3rd param in DateTime::createFromFormat()) is ignored when unix timestamp is passed, so the below code is unnecessary.
$date = DateTime::createFromFormat('U', $timestamp, new DateTimeZone('UTC'); // not needed, 3rd parameter is ignored
You may check PHP's manual for DateTime::createFromFormat for more info and options.
Try this one:
echo date('m/d/Y H:i:s', 1541843467);
$epoch = 1483228800;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00
In the examples above "r" and "Y-m-d H:i:s" are PHP date formats, other examples:
Format Output
r ----- Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date)
c ----- 2017-03-15T12:00:00+01:00 (ISO 8601 date)
M/d/Y ----- Mar/15/2017
d-m-Y ----- 15-03-2017
Y-m-d H:i:s ----- 2017-03-15 12:00:00
Try it.
<?php
$timestamp=1333342365;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
You can try this:
$mytimestamp = 1465298940;
echo gmdate("m-d-Y", $mytimestamp);
Output :
06-07-2016
Unless you need a custom date and time format, it's easier, less error-prone, and more readable to use one of the built-in date time format constants:
echo date(DATE_RFC822, 1368496604);
echo date("l M j, Y",$res1['timep']);
This is really good for converting a unix timestamp to a readable date along with day. Example:
Thursday Jul 7, 2016
echo 'Le '.date('d/m/Y', 1234567890).' à '.date('H:i:s', 1234567890);
I have used this:
<?php echo date('d/m/Y H:i a', $row['start_time']); ?>

String date current date/time?

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);

Categories