convert strtotime to date time format in php - php

i need to convert strtotime to date btime format (from 1307595105 to 06/08/2011 09:51:45 PM PDT) in php
Could you please give me an answer

$unixtime = 1307595105;
echo $time = date("m/d/Y h:i:s A T",$unixtime);
Where
http://php.net/manual/en/function.date.php

<?php
echo date('d - m - Y',strtotime('2013-01-19 01:23:42'));
?>
Out put : 19 - 01 - 2013

Use date() function to get the desired date
<?php
// set default timezone
date_default_timezone_set('UTC');
//define date and time
$strtotime = 1307595105;
// output
echo date('d M Y H:i:s',$strtotime);
// more formats
echo date('c',$strtotime); // ISO 8601 format
echo date('r',$strtotime); // RFC 2822 format
?>
Recommended online tool for strtotime to date conversion:
http://freeonlinetools24.com/

FORMAT DATE STRTOTIME OR TIME STRING TO DATE FORMAT
$unixtime = 1307595105;
function formatdate($unixtime)
{
return $time = date("m/d/Y h:i:s",$unixtime);
}

Here is exp.
$date_search_strtotime = strtotime(date("Y-m-d"));
echo 'Now strtotime date : '.$date_search_strtotime;
echo '<br>';
echo 'Now date from strtotime : '.date('Y-m-d',$date_search_strtotime);

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']); ?>

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

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']); ?>

Timestamp to Date

Ho do I convert:
2010-12-24 11:39:43
to:
24/12 11:39
Thanks.
This should to the trick:
$newFormat = Date ( 'd/m H:i', StrToTime ( '2010-12-24 11:39:43' ) );
You use StrToTime to convert a string representation of a date to timestamp. You then feed that timestamp to the Date function that takes the format of the date as the first parameter.
Try:
$unixtime = strtotime("2010-12-24 11:39:43");
$newFormat = date("d/m H:i", $unixtime);
echo date("d/m H:i", strtotime("2010-12-24 11:39:43"));
$date = DateTime::createFromFormat('Y-m-d G:i:s', 2010-12-24 11:39:43); //You can simply tell DateTime accept your timestamp as is since PHP 5.3
echo $date->format('d/m G:i T'); //Will output what you wanted + Timezone Abbreviation (because of the T)

How to reformat date in PHP?

I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Categories