43037

How to get next Date with specific time in PHP

Question:

I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00". the code i was using get to do that,

<?php $next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00"; $new_trig_time_stamp = strtotime($next_day_date); $trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp); echo $trigger_date_time; ?>

the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00". Is there any other way to do so.

Answer1:

When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)

$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));

Answer2:

Calculate it via unix timestamp - much less annoyance

<?php $trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24); echo $trigger_date_time; ?>

Answer3:

strtotime() is very useful here.

$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );

Answer4:

Have a look at <a href="http://uk.php.net/manual/en/function.date-add.php" rel="nofollow">date_add</a>.

In more detail, something like...

$myDate = new DateTime("Some time"); $myDate->add(new DateInterval("P1D"));

You can then use $myDate->format(…) to extract formatted representations.

Answer5:

echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';

Recommend

  • Enter key event not working on Numeric Input field?
  • Printing on citrix server
  • Pandas plotting two graphs on one scale
  • Fluent Nhibernate: Trying to create entity with composite key that is also the keys for two referenc
  • Need advice in designing tables in SQL-Server
  • MS SQL Server 2008 :Getting start date and end date of the week to next 8 weeks
  • JFreeChart BarChart - Category Markers
  • How does this Java code which determines whether a String contains all unique characters work? [dupl
  • Regex: Match everything except backreference
  • PostgreSQL 9.1 timezones
  • ResponseBuilder is not working when used with entity object
  • Aggregating two data frame columns without any existing pattern logic
  • data.table replicate rows after join?
  • Escaping single quotes in JDBC with MySql
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • How to make R's read_csv2() recognise the text characters properly
  • npm 5.4.1 install/uninstall all failing
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • C: Incompatible pointer type initializing
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • $wpdb not working in file of WordPress plugin
  • Jenkins: How To Build multiple projects from a TFS repository?
  • RectangularRangeIndicator format like triangular using dojo
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • Compare two NSDates in iPhone
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Hits per day in Google Big Query
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?