PHP - strftime (minutes and seconds ignored)? - php

I tried to save time in format YYYY-mm-dd 23:59:59 to mysql database column with datetime. I don't understand why minutes and seconds are ignored always 00 ? Thank you very much for help.
PHP:
$time = strftime('%Y-%m-%d %H:%i:%s', time());
Output:
2014-07-16 11:00:00

You can simple use:
$time = date("Y-m-d H:i:s");
or even better use mysql NOW() function

you have to specify timezone as follows.
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions.

you can use mysql function
now()

You are looking for
$time = strftime('%Y-%m-%d %H:%M:%S', time());
from the documentation
%S Two digit representation of the second
%M Two digit representation of the minute 00 through 59
Also, what PHP version are you using? %i:%s should not be 00:00

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 do I convert date of format like /Date(1490914800000+0100)/ to mysql datetime format in php?

I have tried to solve it by extracting the numeric part and then parsed it using date function. But it shows me some old date which I guess is not correct.
$datef = "1490914800000+0100";
$adada = date('Y-m-d H:i:s', $datef);
// Gives date 1987-10-13 18:31:28 which is an old date. Please suggest.
One approach, well-covered by this SO question, is to use the DateTime() function to convert time in seconds since epoch to a date, and then display this date using format(). But there are two caveats with your data. First, you appear to have milliseconds since the epoch, which needs to be converted to seconds. Second, you also have a timezone shift, in hours, tagged to the end. I split your $datef string into two parts, epoch and timezone, then arrive at the number of seconds since epoch.
list($epoch, $timezone) = explode('+', $datef);
$epoch = ($epoch / 1000) + (substr($timezone, 0, 2)*60*60) +
(substr($timezone, 2, 2)*60);
$dt = new DateTime("#$epoch");
echo $dt->format('Y-m-d H:i:s');
Output:
2017-03-31 00:00:00
Demo here:
PHP Sandbox
The time seems to be in milliseconds.
You can add the timezone shift to the seconds. 1 hour = 3600 seconds.
$milliSeconds = intval("1490914800000");
$seconds = $milliSeconds/1000;
$date = date("Y-m-d H:i:s", $seconds);

how to convert 12h to 24h in php

I want to convert 12h time in php which is in this format: 05/31/2012 09:48 AM
to 24h time format : 2011-11-27 11:53:36
This is my code line that you can change:
$time= $_POST['time'];
You can use this:
// $time = 05/31/2012 09:48 AM
$time = $_POST['time'];
//output new time: 2012-05-31 09:48:00
$newTime = date("Y-m-d H:i:s", strtotime($time)) );
live example
Doc:
strtotime
date
http://psoug.org/snippet/Convert_12_to_24_hour_time_and_vice_versa_241.htm
You have to use strtotime like this:
$date="05/31/2012 09:48 AM";
echo date("Y-m-d H:i:s",strtotime($date));
go through below link for date functionality in PHP
http://php.net/manual/en/function.date.php
You can use strtotime and then date functions for that.
http://php.net/manual/en/function.date.php All the possible options are listed there.
The first argument to date is the format and the second is a timestamp (which you get with strtotime of the current date string you have).
date('Y-m-d H:i:s', strtotime($your_current_date_string));
The format is just off the top of my head and not exact. You can look up the format you need in the table in the link provided.

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

Comparing date("Y-m-d h:i:s");

how can I make a conditional statement like if date("Y-m-d h:i:s"); is more than 30 seconds after date("Y-m-d h:i:s");.
I've previously used something like date("Y-m-d h:i:s"); < date("Y-m-d h:i:s"); + 30, however this doesn't seem to work.
Help?
Use strtotime() to convert them to UNIX time.
E.g.:
if(strtotime($date2) - strtotime($date1) > 30) {
// $date2 is more than 30 seconds after $date1
}
or
if(abs(strtotime($date2) - strtotime($date1)) > 30) {
// $dates are more than 30 second apart
}
Use strtotime() to convert both dates to a unix timestamp, then add the wanted amount of seconds to the second and do an integer comparison.
Use unixtime, instead. Just convert your date("Y-m-d h:i:s"); to date("U") or just time().
strototime() could help you with this.
It shows the time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Then this method $unixtime + 30 will work fine as well ;)
It looks like you are trying to compare database date fields (like mysql DATETIME). Use the database systems date and time functions to compare date values (like DATE_ADD() or + and with simple < or > in MySQL).

Categories