How to increment a MySQL timestamp in PHP? - php

So I have a MYSQL timestampl that is generating values that are 4hours ahead of what they should be. How would i subtract 4hrs from the MySQL time stamp and display that?
Here's my current code
$mdate = date('F j, Y, g:i a',strtotime( $bm->date_added));
//$bm->dated_added returns the times stamp//
echo $mdate;
Thanks

When supplied with a second argument, strtotime() will perform calculations based on this date:
$mdate = date('F j, Y, g:i a', strtotime('-4 hours', $bm->date_added));
strtotime() can perform several calculations base on modifiers like these. Examples from the manual:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

You should really figure out the root problem and fix it. Most likely you need to set MySQL's timezone to fit your own (or the server itself has the wrong date set).
That said, you can just subtract 14,400 seconds from the strtotime result.
$mdate = date('F j, Y, g:i a',strtotime($bm->date_added) - 14400);

Use the SUBTIME() function in your query if you cannot change the server time to reflect a appropriate timestamp

SQL approach:
SELECT TIMESTAMPADD(the_timestamp, INTERVAL -4 HOURS)
This will return you the offset time

Related

How to get the following date '1333504225' in to '0000-00-00' with php?

For some reason this is how I get the date. I have tried using php to decode it but I get the wrong date.
$datetst ='1333504225';
$date = strtotime($datetst);
echo date("F j, Y", $date);
What I plan to do with this is calculate the difference in days from (today-the day the record was created). Which is why I need to have it in '0000-00-00'. I am no expert, but our engineer is of no help.
No need to convert your unix timestamp again by strtotime
just use this
$datetst =1333504225;
echo date("F j, Y", $datetst);
and to get difference in days use below code
$datetst = 1333504225;
echo "Date : ".date("F j, Y", $datetst);
$diff = time() - 1333504225 ;
$diffDay = floor($diff/(3600*24));
echo "\nDays from now : ".$diffDay;
Demo link : https://eval.in/716084

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.

How to add months to a particular date

I just hope this question won't be marked as a duplicate because I've seen similar questions on stackoverflow but they all talk about adding days to the date, The problem here is that i want to add some particular months to a particular date which is gotten from my database I've tried adding it using strtotime() but the date just returns 1st January 1970, the code looks like this
<?php echo date('jS F Y', strtotime("$date +1 month")); ?>
//This is the value of date
$date = $student->date;
How to I add months to this particular date? Please note that the date is a timestamp in my database.Thanks
You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.
$date = new DateTime('#'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');
If you want to stick to using date() and strtotime() you would use this:
echo date("jS F Y", strtotime("+1 month", $student->date));
strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.
You should check out the documentation here,
But the just of it is the $date->add function. It allows you to add any amount of time to a timestamp using a DateInterval. Its a little tricky to get used to but here are a couple of examples:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
which outputs:
2000-01-01 10:00:30
2007-06-05 04:03:02
The date interval is formatted in years months days hours minuets seconds, simply put in the amount you want and it will add it, so in your case:
<?php
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
PHP's strtotime() function allows for a second parameter that allows you to set a relative date.
If you would like to add a month to tomorrow, here's how:
<?php
echo date("jS F Y", strtotime("+1 month", strtotime("2014-10-09")));
// returns: 9th November 2014

php filemtime 24 hr format

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

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