php filemtime 24 hr format - php

I created this code to get the date a file was last touched then display it to the user in AM/PM format.
It doesn't seem to be working though. I know I'm close; what am I doing wrong?
$filename = 'test.html';
if (file_exists($filename)) {
$date = date(filemtime($filename));
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', strtotime($date));
Output: last updated: December 31 1969 06:59 PM

Try this:
if (file_exists($filename)) {
$date = filemtime($filename);
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', $date);
In your code, this line:
$date = date(filemtime($filename));
wouldn't work since filemtime returns a UNIX timestamp, which you are then passing as the first parameter to date(). Even if that did work, you are then converting that date back to a UNIX timestamp with strtotime(), and then back into a date string again which seems a little inefficient.
Also consider what happens if the file doesn't exist, will $date have been set elsewhere in your code?

$date = date(filemtime($filename));
That line is wrong. First argument to date() is a format string. Replace with:
$date = filemtime($filename);
Also, you don't need to perform strtotime() on a timestamp, just use as is:
echo date('F d Y h:i A', $date);

Related

how to get time from timestamp using carbon php

I have a problem in extracting time from $timestamp which something looks like
this $timestamp = "2017-05-03 11:47:48";
my question is: how to extract time from above $timestamp like this 11:47 AM
my current time zone is Asia/Kolkata
First make sure you installed carbon, refer to https://github.com/briannesbitt/Carbon.
$timestamp = "2017-05-03 11:47:48";
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2017-05-03 11:47:48', 'Asia/Kolkata');
echo $date->format("H:i A");
First of all this
$timestamp = "2017-05-03 11:47:48";
is not timestamp, Its a date time value in some specific format. To change it to your required format you can try following code in Php:
echo date('H:i A', strtotime($timestamp));
here H is used for 24 Hour format, use h for 12 hours format.
Hope this will help you out.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$timestamp = "2017-05-03 11:47:48";
$dateObj=date_create_from_format("Y-m-d H:i:s", $timestamp);
echo $dateObj->format("H:i A");
Output:
11:47 AM

strtotime() Is Adding An Extra Day

Can anybody tell me why strtotime() seems to be adding 1 day? This seems to only happen in the late afternoon (something like 7 or 8 PM), otherwise it says the correct day.
echo date('m/d/Y h:i:s a', time());
Output:
12/21/2015 08:34:43 pm
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
Output:
Tuesday, December 22nd, 2015
I would like the above output, however, I want today's date (the 21st not the 22nd).
Use date instead of gmdate.
You are using gmdate() which gets the date in UTC. The problem only happens late in the afternoon/evening because at those times it really is the next day in UTC time.
You're also doing too much work - you can simplify that line of code to this:
// echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
echo date('l, F jS, Y');
Otherwise you've created a timestamp from a time string based on the current time stamp. You could just leave the second parameter to date empty and the current time "now" is assumed.
It is also very important to make sure you are calling date_default_timezone_set somewhere or that you have it configured in your php.ini.
This detail in your code...
echo gmdate('l, F jS, Y', strtotime(date('m/d/Y h:i:s a', time())));
(= the "gmdate") will always return Greenwich Mean Time (GMT), which is London/UK.
So change that to date(....
And add date_default_timezone_set('America/New_York'); anyway...
Decided to ultimately use:
$date = new DateTime(date('Y-m-d'), new DateTimeZone('America/New_York'));
$timestamp = $date->format('U');
$date = gmdate('l, F jS, Y', $timestamp);
based on Alexander's comment.

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

strtotime not working with TIME?

My mysql column has this datetime value, 2011-04-11 11:00:00 when I am applying strtotime then its returning date less than today,whereas it should be greater than today.
also when I am trying this strtotime(date('d/m/Y h:i A')); code, its returning wrong values. Is there any problem with giving TIME in strtotime?
Basically I want to do, is to compare my mysql column date with today's date, if its in future then show "Upcoming" else show nothing?
Please help and advise, what should I do?
Edited code
$_startdatetime = $rs['startdatetime'];
$_isUpcoming = false;
if(!empty($_startdatetime)){
$TEMP_strtime = strtotime($_startdatetime);
$TEMP_strtime_today = strtotime(date('d/m/Y h:i A'));
if($TEMP_strtime_today < $TEMP_strtime){
$_isUpcoming = true;
$_startdatetime = date('l, d F, Y h:i A' ,$TEMP_strtime);
}
}
And the value in $rs['startdatetime'] is 2011-04-11 11:00:00. And with this value I am getting following output.
$TEMP_strtime - 1302519600
$TEMP_strtime_today - 1314908160
$_startdatetime - 2011-04-11 11:00:00
$_startdatetime its value is not formatted as the upcoming condition is false, so returning as is mysql value.
d/m/Y h:i A is irreversible (with strtotime) format, use standard formats or use time() as recommended by Joel & Rocket
PROBLEM
<?php
echo $today = date('d/m/Y h:i A');
echo '<br />';
echo $time = strtotime($today);
echo '<br />';
echo date('d/m/Y h:i A', $time);
OUTPUT
SOLUTION
<?php
$today = strtotime(date('m/d/Y h:i:s')); <- use appropriate format
// OR
$today = time(); #credit to Joel & Rocket

How to get AM/PM from a datetime in PHP [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM

Categories